218

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martynas B
  • 2,843
  • 2
  • 12
  • 15

9 Answers9

371

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'
Adam Burley
  • 5,551
  • 4
  • 51
  • 72
laalto
  • 150,114
  • 66
  • 286
  • 303
  • 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 04 '20 at 17:03
  • This seems to have to be `implementation` not `dependency`? – Richard Barraclough Jun 27 '20 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. – Richard Barraclough Jun 27 '20 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? – Edward Falk Aug 03 '20 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 03 '20 at 19:57
  • Where in the build.grandle I add this line? What happens with older mobile phones if I change "android." to "androidx."? – Chameleon Aug 26 '20 at 18:09
  • What about Xamarin.Android. I've tried AndroidX.Preferences; but it doesn't exist. – Pavlos Mavris Dec 28 '22 at 09:30
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.

tronman
  • 9,862
  • 10
  • 46
  • 61
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
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"
Androidcoder
  • 4,389
  • 5
  • 35
  • 50
  • 2
    This is a good answer, as context.getPackageName() also works in modules and retrieves the application-id. – Robert Apr 19 '21 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) – James Smith Apr 30 '21 at 18:36
  • 3
    Nice way to avoid unnecessarily using androidx.preference.PreferenceManager just to access getDefaultSharedPreferences. – A.J. Aug 22 '21 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 07 '22 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.

Ngima Sherpa
  • 1,397
  • 1
  • 13
  • 34
  • 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. – Reyhane Farshbaf Nov 25 '21 at 07: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.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
3

Kotlin library

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

Kotlin use

Configuration.getInstance().load(this, androidx.preference.PreferenceManager.getDefaultSharedPreferences(this))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
developerjavad
  • 315
  • 1
  • 2
  • 10
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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Anupam
  • 2,845
  • 2
  • 16
  • 30
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)
Crebain
  • 180
  • 1
  • 8