PreferenceManager getDefaultSharedPreferences
is deprecated in Android Q, how do I replace it?
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.
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.
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
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"
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'
kotlin libray
implementation 'androidx.preference:preference-ktx:1.1.1'
kotlin use Configuration.getInstance().load(this,androidx.preference.PreferenceManager.getDefaultSharedPreferences(this))