PreferenceManager getDefaultSharedPreferences deprecated in Android Q
Asked Answered
B

10

229

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

Boulogne answered 1/7, 2019 at 10:0 Comment(0)
G
394

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'
Greenlaw answered 1/7, 2019 at 10:6 Comment(7)
@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.Greenlaw
This seems to have to be implementation not dependency?Bobbee
Not that that works either: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not find implementation "androidx.preference:preference:1.1.0.Bobbee
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?Debase
@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.Greenlaw
Where in the build.grandle I add this line? What happens with older mobile phones if I change "android." to "androidx."?Betthezel
What about Xamarin.Android. I've tried AndroidX.Preferences; but it doesn't exist.Tetracaine
D
202

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.

Dare answered 6/7, 2019 at 5:30 Comment(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.Dare
Can we use in APIs before API 29?Realist
@Realist yes, you can.Nucleonics
T
25

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"
Tristis answered 14/7, 2020 at 14:34 Comment(4)
This is a good answer, as context.getPackageName() also works in modules and retrieves the application-id.Fireweed
Also note ; for java and not for Kotlin. Kotlin it would look like something like getSharedPreferences(applicationContext.packageName, Context.MODE_PRIVATE)Polysyndeton
Nice way to avoid unnecessarily using androidx.preference.PreferenceManager just to access getDefaultSharedPreferences.Lyceum
also If your are calling it from outside an activity, use "context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)"Aleksandropol
T
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.

Tripitaka answered 27/9, 2021 at 10:50 Comment(1)
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.Jobina
H
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.

Hymettus answered 1/7, 2019 at 10:4 Comment(2)
What wasn't consistent about the original one?Condemnation
@androiddeveloper it is obvious, missing letter x in the package nameVenezuela
S
3

Kotlin library

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

Kotlin use

Configuration.getInstance().load(this, androidx.preference.PreferenceManager.getDefaultSharedPreferences(this))
Striate answered 18/11, 2020 at 8:12 Comment(0)
R
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

Resemblance answered 1/7, 2019 at 10:6 Comment(0)
C
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.

Carrageen answered 22/9, 2021 at 8:38 Comment(0)
T
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)
Tarahtaran answered 23/9, 2021 at 12:4 Comment(0)
S
1

You have to import androidx library like

import androidx.preference.PreferenceManager

For kotlin DSL:

implementation("androidx.preference:preference:1.2.1")

For KSP:

implementation("androidx.preference:preference-ktx:1.2.1")

For more info, you can visit official document of Preference.

Sargasso answered 7/11, 2023 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.