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...
}
getFragmentManager() Return the FragmentManager for interacting with fragments associated with this fragment's activity.
Deprecated: Deprecated in Java.
The Fragment class has two callback methods, onAttach() and onDetach() , that you can override to perform work when either of these events occur.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With