PreferenceManager getDefaultSharedPreferences deprecated in Android Q

后端 未结 6 1996
深忆病人
深忆病人 2021-01-30 04:32

PreferenceManager getDefaultSharedPreferences is deprecated in Android Q, how do I replace it?

相关标签:
6条回答
  • 2021-01-30 05:12

    Quote from PreferenceManager documentation:

    This class was deprecated in API level 29.
    Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.

    0 讨论(0)
  • 2021-01-30 05:15

    Package preference provides the androidx PreferenceManager:

    Java: implementation "androidx.preference:preference:1.1.1"

    Kotlin: implementation "androidx.preference:preference-ktx:1.1.1"


    i.e. change android.preference.PreferenceManager to androidx.preference.PreferenceManager


    Also see PreferenceFragmentCompat, which is the current PreferenceFragment class to use.

    0 讨论(0)
  • 2021-01-30 05:17

    Yes, it is deprecated. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.

    Follow this -

    PreferenceManager

    0 讨论(0)
  • 2021-01-30 05:25

    If you're just saving and retrieving key-value pairs you can replace:

     prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    

    with:

     prefs = getSharedPreferences(
                "my.app.packagename_preferences", Context.MODE_PRIVATE);
    

    Be sure to use the right file name for the new implementation or your users will lose access to everything saved with getDefaultSharedPreferences(!). The following will get the file name getDefaultSharedPreferences uses:

    getPackageName() + "_preferences"
    
    0 讨论(0)
  • 2021-01-30 05:27

    You can use AndroidX support library version of PreferenceManager, i.e. androidx.preference.PreferenceManager and not android.preference.PreferenceManager.

    remember to add the following to your build.gradle.

    implementation 'androidx.preference:preference:1.1.1'
    
    0 讨论(0)
  • 2021-01-30 05:29

    kotlin libray

    implementation 'androidx.preference:preference-ktx:1.1.1'
    

    kotlin use Configuration.getInstance().load(this,androidx.preference.PreferenceManager.getDefaultSharedPreferences(this))

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