Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...
    }
like image 989
Peter Lenjo Avatar asked Sep 16 '18 12:09

Peter Lenjo


People also ask

What is getFragmentManager?

getFragmentManager() Return the FragmentManager for interacting with fragments associated with this fragment's activity.

Is Fragment Manager deprecated?

Deprecated: Deprecated in Java.

What are the methods you can override in the fragment class?

The Fragment class has two callback methods, onAttach() and onDetach() , that you can override to perform work when either of these events occur.

What are fragments android?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


1 Answers

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

like image 93
Jitendra A Avatar answered Nov 07 '22 13:11

Jitendra A