PreferenceManager getDefaultSharedPreferences deprecated in Android Q

AndroidSharedpreferencesAndroid 10.0

Android 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 -

https://developer.android.com/reference/androidx/preference/package-summary.html">PreferenceManager</a>

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartynas BView Question on Stackoverflow
Solution 1 - AndroidlaaltoView Answer on Stackoverflow
Solution 2 - AndroidMartin ZeitlerView Answer on Stackoverflow
Solution 3 - AndroidAndroidcoderView Answer on Stackoverflow
Solution 4 - AndroidSergey GlotovView Answer on Stackoverflow
Solution 5 - AndroidNgima SherpaView Answer on Stackoverflow
Solution 6 - AndroidAnupamView Answer on Stackoverflow
Solution 7 - AndroiddeveloperjavadView Answer on Stackoverflow
Solution 8 - AndroidS.M. Zahidul IslamView Answer on Stackoverflow
Solution 9 - AndroidCrebainView Answer on Stackoverflow