PreferenceManager getDefaultSharedPreferences
is deprecated in Android 10. How do I replace it?
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.2.0'
implementation
not dependency
? –
Bobbee getDefaultSharedPreferences()
is collateral damage. Yes, many preferemces-related areas were fragmented so it makes sense to move them to a support library. getDefaultSharedPreferences()
implementation itself is still the same in both Android platform and AndroidX libraries, so that function did not really need deprecation. –
Greenlaw 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.
PreferenceFragmentCompat
, which didn't exist back then... it's usually then next one deprecation problem one might face, when trying to update preferences. The other linked answer was only valid for a limited amount of time. –
Dare 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"
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.
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.
Kotlin library
implementation 'androidx.preference:preference-ktx:1.1.1'
Kotlin use
Configuration.getInstance().load(this, androidx.preference.PreferenceManager.getDefaultSharedPreferences(this))
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 -
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.
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)
You have to import androidx library like
import androidx.preference.PreferenceManager
For kotlin DSL:
implementation("androidx.preference:preference:1.2.1")
For KSP:
implementation("androidx.preference:preference-ktx:1.2.1")
For more info, you can visit official document of Preference.
© 2022 - 2024 — McMap. All rights reserved.