Usually in the examples it is advised to make the settings screen like this:
Create a file pref_headers.xml of approximately the following content:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > <header android:fragment="ru.my.proj.MActivity$PrefFragment1" android:title="@string/pref_header_1" /> <header android:fragment="ru.my.proj.MActivity$PrefFragment2" android:title="@string/pref_header_2" /> </preference-headers> 

And for each category of settings, create your own Fragment of the form:

 public static class PrefFragment1 extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.pref_1); } } 

But if there are a lot of such categories, is it not better to describe one Fragment with its constructor:

 public static class PrefFragment extends PreferenceFragment { private int resource; public PrefFragment(int res) { resource = res; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(resource); } } 

But how now, from pref_headers.xml, call the constructor described above?

    1 answer 1

    Still, I found the answer to the question in the official documentation in the middle of this document .
    In short, you can add an extra element to the header , which is a key-value pair:

     <preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > <header android:fragment="ru.my.proj.MActivity$PrefFragment1" android:title="@string/pref_header_1" > <extra android:name="category" android:value="Category1"/> </header> <header android:fragment="ru.my.proj.MActivity$PrefFragment2" android:title="@string/pref_header_2" > <extra android:name="category" android:value="Category2"/> </header> </preference-headers> 

    And in the PrefFragment you can check it with the getArguments () method:

     public static class PrefFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String settings = getArguments().getString("category"); if ("Category1".equals(settings)) { addPreferencesFromResource(R.xml.pref_1); } else if ("Category2".equals(settings)) { addPreferencesFromResource(R.xml.pref_2); } } } 

    It turned out quite simple!