what to replace deprecated getFragmentManager() method with (API 28)?
Asked Answered
M

1

10

I have an Android application I've been working on, min sdk = 21. In it, I use a custom PreferenceActivity that in turn calls a PreferenceFragment. However, after recently updating to API 28, I noticed that the getFragmentManager() method has been deprecated. I skimmed through the relevant Android Developers page where I learnt that the Fragment class has been deprecated, or something of the sort. For clarity, my PreferencesActivity class code is as follows:

public class PreferencesActivity extends PreferenceActivity{
static int layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        boolean isDark = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("darkTheme", false);
        if (isDark) setTheme(R.style.AppTheme_Dark_NoActionBar_BlendStatusBar);
        layout = isDark? R.style.AlertDialogCustom_Dark: R.style.AlertDialogCustom;
        super.onCreate(savedInstanceState);
        // TODO: 08/09/2018 Deprecated
        getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesFragment()).commit();
}

    public static class PreferencesFragment extends PreferenceFragment {
        // Preferences code here...
    }
Menado answered 16/9, 2018 at 12:59 Comment(1)
W
10

Yes you are correct. getFragmentManager and android.app.Fragment were deprecated in API 28. Instead now we should use Fragment from support library and getSupportFragmentManager respectively.

But if you are worried about those deprecations then you should also note that PreferenceFragments too were deprecated in API 28.

Instead, you should use PreferenceFragmentCompat

Whipsaw answered 16/9, 2018 at 13:48 Comment(1)
I'll try this out and get back to youMenado

© 2022 - 2024 — McMap. All rights reserved.