PreferenceManager getDefaultSharedPreferences deprecated in Android Q
AndroidSharedpreferencesAndroid 10.0Android Problem Overview
PreferenceManager getDefaultSharedPreferences
is deprecated in Android 10. How do I replace it?
Android Solutions
Solution 1 - Android
You can use the Android 10 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'
Solution 2 - Android
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.
Solution 3 - Android
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"
Solution 4 - Android
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.
Solution 5 - Android
Use Jetpack DataStore, It is a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers. DataStore uses Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally.
> > If you're currently using SharedPreferences to store data, consider > migrating to DataStore instead. > -
Setup
dependencies {
implementation "androidx.datastore:datastore:1.0.0"
}
It also has support for RxJava2 & RxJava3.
Solution 6 - Android
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 -
Solution 7 - Android
Kotlin library
implementation 'androidx.preference:preference-ktx:1.1.1'
Kotlin use
Configuration.getInstance().load(this, androidx.preference.PreferenceManager.getDefaultSharedPreferences(this))
Solution 8 - Android
You can import this library at app level gradle
implementation "androidx.preference:preference-ktx:1.1.1"
Then remove imported file from class where you create "PreferenceManager" Press Alt+Enter and import androidx hope you get latest version of preference manager.
Solution 9 - Android
implementation "androidx.preference:preference-ktx:1.1.1"
class file PrivateSharedPreferences;
class PrivateSharedPreferences(context: Context) {
private val file = "com.example.com_shared"
private val key = "private_key"
private var sharedPreferences = context.getSharedPreferences(file, Context.MODE_PRIVATE)
private val editor = sharedPreferences.edit()
fun save(ok: Boolean) {
editor.putBoolean(key, ok)
editor.apply()
}
fun read() : Boolean {
return sharedPreferences.getBoolean(key, false)
}
}
read from fragment or adapter;
PrivateSharedPreferences(context).read()
save
PrivateSharedPreferences(context).save(true)