Martynas B
Martynas B

Reputation: 3143

PreferenceManager getDefaultSharedPreferences deprecated in Android Q

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

Upvotes: 242

Views: 95699

Answers (10)

Rumit Patel
Rumit Patel

Reputation: 12539

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.

Upvotes: 2

laalto
laalto

Reputation: 152867

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'

Upvotes: 414

Anupam
Anupam

Reputation: 2853

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

Upvotes: 1

Ngima Sherpa
Ngima Sherpa

Reputation: 1555

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.

Upvotes: 12

Androidcoder
Androidcoder

Reputation: 4679

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"

Upvotes: 34

Crebain
Crebain

Reputation: 290

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)

Upvotes: 1

S.M. Zahidul Islam
S.M. Zahidul Islam

Reputation: 21

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.

Upvotes: 1

developerjavad
developerjavad

Reputation: 332

Kotlin library

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

Kotlin use

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

Upvotes: 3

Martin Zeitler
Martin Zeitler

Reputation: 76779

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.

Upvotes: 205

Sergey Glotov
Sergey Glotov

Reputation: 20346

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.

Upvotes: 2

Related Questions