219

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

9 Answers 9

376

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'
7
  • 1
    @AbhinavSaxena Yes of course. That's implicit when using a support library. An earlier edit to this answer added the kotlin-only ktx depency; I rolled back that edit because it was kotlin-only.
    – laalto
    Feb 4, 2020 at 17:03
  • This seems to have to be implementation not dependency? Jun 27, 2020 at 14:42
  • Not that that works either: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not find implementation "androidx.preference:preference:1.1.0. Jun 27, 2020 at 14:43
  • I'm curious; why did they deprecate that interface, and was some other way of obtaining shared preferences preferred, or was switching to androidx the intent? Aug 3, 2020 at 17:56
  • 1
    @EdwardFalk I don't know but to me it smells like 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.
    – laalto
    Aug 3, 2020 at 19:57
200

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.

3
  • Added a direct link to 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. Apr 12, 2020 at 22:30
  • Can we use in APIs before API 29? Jun 25, 2020 at 18:20
  • @H.Karatsanov yes, you can.
    – Luis
    Jun 24, 2021 at 17:55
23

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"
4
  • 2
    This is a good answer, as context.getPackageName() also works in modules and retrieves the application-id.
    – Robert
    Apr 19, 2021 at 18:08
  • 2
    Also note ; for java and not for Kotlin. Kotlin it would look like something like getSharedPreferences(applicationContext.packageName, Context.MODE_PRIVATE) Apr 30, 2021 at 18:36
  • 3
    Nice way to avoid unnecessarily using androidx.preference.PreferenceManager just to access getDefaultSharedPreferences.
    – A.J.
    Aug 22, 2021 at 17:16
  • 2
    also If your are calling it from outside an activity, use "context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)"
    – Angel Koh
    Jun 7, 2022 at 14:52
12

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.

1
  • 2
    Sometimes we have one choice and it's only shareprefrences, for example when you use third party libraries that receive shareprefrences as parameter called on some method. open street map configuration load method is a case. Nov 25, 2021 at 7:53
3

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.

1
  • 39
    What wasn't consistent about the original one? Dec 10, 2019 at 9:01
3

Kotlin library

implementation 'androidx.preference:preference-ktx:1.1.1'

Kotlin use

Configuration.getInstance().load(this, androidx.preference.PreferenceManager.getDefaultSharedPreferences(this))
2

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 -

PreferenceManager

1

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.

1
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.