Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceManager getDefaultSharedPreferences deprecated in Android Q

PreferenceManager getDefaultSharedPreferences is deprecated in Android 10. How do I replace it?

like image 574
Martynas B Avatar asked Oct 15 '22 02:10

Martynas B


People also ask

What is PreferenceManager in android?

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.

How do I use Kotlin shared preferences?

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.

How is data stored in shared preferences?

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.


2 Answers

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'
like image 343
laalto Avatar answered Oct 17 '22 15:10

laalto


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.

like image 195
Martin Zeitler Avatar answered Oct 17 '22 16:10

Martin Zeitler