PreferenceManager getDefaultSharedPreferences
is deprecated in Android 10. How do I replace it?
PreferenceManager: Used to help create Preference hierarchies from activities or XML. SharedPreferences: Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share.
This example demonstrates how to use shared preferences in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With