He studied the book "Android. Programming for professionals", 2nd edition.
There in all projects 2 SingleFragmentActivity files are SingleFragmentActivity :

 public abstract class SingleFragmentActivity extends AppCompatActivity { protected abstract Fragment createFragment(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragment_container); if (fragment == null) { fragment = createFragment(); fm.beginTransaction() .add(R.id.fragment_container, fragment) .commit(); } } } 

activity_fragment:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/> 

How to set up AndroidStudio so that when creating a project there were exactly these files, is it possible to create your own template or other options?

In addition to these files, there must also be one file, the main MainActivity, which is created when creating a project according to the standard.

  • one
    Perhaps, instead of creating a whole project template of your own (which is rather a chore), you will only need the built-in Android Studio tool - File and Code Templates, which allows you to make a template of a separate file (class) and load it entirely. Instructions. That is, to get the whole class you need to click on one button. - pavlofff

1 answer 1

In the source code of the SDK there is a description of the way to solve the problem. Android IDE Template Format.

To create templates used FreeMarker. This is very similar to using PHP and Django templates.

The bottom line is that we have a specific template and form for entering parameters. After input, the parameters are transferred to the template, and the required class is collected based on this data.

The list of all used templates is located at the following address android-studio-folder \ plugins \ android \ lib \ templates \

To create your template, add to the directory of activities (or other) a new directory with the following list of files list of files

Only the template.xml and recipe.xml files are required.

Read more: article on Habr .