PreferenceActivity: getFragmentManager() has been deprecated

非 Y 不嫁゛ 提交于 2020-01-20 06:54:06

问题


when testing a android.preference.PreferenceActivity, I get the following warning:

warning: [deprecation] getFragmentManager() in Activity has been deprecated

that is how I obtain a handle to the current PreferenceFragment:

FragmentManager fm = this.mActivity.getFragmentManager();
this.currentFragment = (PreferenceFragment) fm.getFragments().get(1);

using FragmentActivity.getSupportFragmentManager() is obviously not an option.

found PreferenceFragmentCompat, which would replace the deprecated PreferenceFragment.

but, is there any androidx replacement for the PreferenceActivity?


回答1:


The Fragment-related UI classes that ship as part of the device firmware have been deprecated in Android 28. It is recommended to move to the support library classes for Activitys and Fragments.

There are already other posts about this:

  • Since the Android getFragmentManager() API is deprecated, is there any alternative?
  • How to use the v7/v14 Preference Support library?



回答2:


what I've meanhile came up with ...is an AppCompatActivity, which inflates androidx PreferenceFragment. I've kept the previous EXTRA_SHOW_FRAGMENT, so that switching to a specific PreferenceFragment still would work. EXTRA_NO_HEADERS has not yet been considered:

/**
 * Preference {@link AppCompatActivity}.
 * @see <a href="https://developer.android.com/reference/androidx/preference/Preference Fragment">PreferenceFragment</a>
**/
public final class PreferenceCompatActivity extends AppCompatActivity {

    /** the class-name of the main {@link androidx.preference.PreferenceFragment} */
    public static final String MAIN_FRAGMENT = "com.acme.fragment.PreferencesFragment";

    /** framework {@link Intent} extra */
    public static final String EXTRA_SHOW_FRAGMENT = ":android:show_fragment";

    /** framework {@link Intent} extra */
    public static final String EXTRA_NO_HEADERS = ":android:no_headers";

    /** the currently displayed {@link PreferenceFragment} */
    private PreferenceFragment currentFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String fragmentName = MAIN_FRAGMENT;
        Intent intent = getIntent();
        if (intent.getStringExtra(EXTRA_SHOW_FRAGMENT) != null) {
            fragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
        }
        this.switchToFragment(fragmentName, null);
    }

    private void switchToFragment(String fragmentName, @Nullable Bundle args) {
        PreferenceFragment fragment;
        switch(fragmentName) {
            // case "": {break;}
            default: {
                fragment = new PreferencesFragment();
            }
        }
        getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
        this.currentFragment = fragment;
    }

    @VisibleForTesting(otherwise = VisibleForTesting.NONE)
    public PreferenceFragment getCurrentFragment() {
        return this.currentFragment;
    }

    ...
}


来源:https://stackoverflow.com/questions/53078533/preferenceactivity-getfragmentmanager-has-been-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!