0

In my Android app, I want to make a preference activity using PreferenceActivity because I am addressing phones where androidx is not available (PreferenceActivity is indeed not deprecated until API 28). However, the essential method addPreferencesFromResource(), which I saw being used in nearly every tutorial on that, is deprecated in API 15 already. The same applies for other methods.

How to use PreferenceActivity in APIs 15 to 28? Is there a tutorial somewhere or can this be explained briefly? Thank you!

Kolodez
  • 553
  • 2
  • 9
  • ```PreferenceFragment``` https://stackoverflow.com/questions/45066520/whats-the-proper-way-to-setup-an-android-preferencefragment – private static Dec 19 '20 at 08:38
  • Thanks. But I don't quite understand the answer https://stackoverflow.com/a/45067104/10126596 because it contains only a few code snippets and seems to be adapted to the question author who already knows much more about this topic than me. The answer https://stackoverflow.com/a/51854023/10126596 seems not to be appropriate because it uses `PreferenceFragmentCompat` which is a part of `androidx` according to https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat. – Kolodez Dec 19 '20 at 10:33

1 Answers1

0

I found out. Here are links to documentations of some old APIs. I looked up at docs-19_r02/docs/guide/topics/ui/settings.html#Fragment:

public class ActivityPreferences extends Activity {
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace (android.R.id.content, new FragmentPreferences());
        transaction.commit();
    }
}

public class FragmentPreferences extends PreferenceFragment {
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        this.addPreferencesFromResource (R.xml.preferences);
    }
}
Kolodez
  • 553
  • 2
  • 9