Alternatives to PreferenceFragment with android-support-v4

后端 未结 9 2058
误落风尘
误落风尘 2020-11-28 04:50

I\'ve come to a sudden halt in the development of my app as I realized that PreferenceFragments weren\'t supported in this library. Are there any alternatives that a rookie

相关标签:
9条回答
  • 2020-11-28 05:17

    You coul maybe use a real activity and use fragment, but I don't think it's a good choice. You should use simple PreferenceActivity and wait for improvements in retro compat libs.

    0 讨论(0)
  • 2020-11-28 05:20

    As Lucius said you can use PreferenceFragmentCompat :

    public class MyPreferenceFragment extends PreferenceFragmentCompat {
            @Override
            public void onCreatePreferences(Bundle bundle, String s) {               
                PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
                addPreferencesFromResource(R.xml.preferences);
           }
    }
    

    You must include its dependency in gradle:

    dependencies {
    ...
        compile 'com.android.support:preference-v7:23.1.X' (wherX = 0,1,...)
    ...
    }
    

    This way you can also can use FragmentTransaction of android.support.v4.app.FragmentTransaction and PrefernceFragment. However, you maybe have problems with themes. If it is the case, you can solve it having into account this post:

    Solution to make the lib work on API 7+ while keeping material theme on API 14+

    0 讨论(0)
  • 2020-11-28 05:25

    You can use third party libraries such as UnifiedPreferences to use a single API for all versions of Android.

    0 讨论(0)
提交回复
热议问题