SharedPreferences
interface SharedPreferences
| android.content.SharedPreferences |
Interface for accessing and modifying preference data returned by Context.getSharedPreferences. For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the various get methods must be treated as immutable by the application.
Note: The Android team strongly recommends against using SharedPreferences for new data storage needs. Instead, consider using Jetpack DataStore for storing small amounts of data, or Room for relational data and larger datasets.
There are several significant drawbacks to using SharedPreferences:
- UI Thread Blocking and ANRs: Since
SharedPreferencesprovides a synchronous API, reading and writing to disk can block the UI thread, leading to jank and StrictMode violations. Additionally, pendingEditor.apply()calls will block the main thread during component lifecycle transitions (such as when Activities or Services start or stop) to ensure data is persisted. This is a common source of Application Not Responding (ANR) errors. - Error Handling: The asynchronous
Editor.apply()method has no mechanism to signal errors to the caller. The synchronousEditor.commit()method only returns a boolean, providing no detailed information about failures, and can sometimes returnfalseeven when a write succeeds. - Durability and Consistency: Changes are reflected in memory immediately, before being successfully persisted to disk. This can result in data loss if the app crashes or is terminated. For instance, a user might change a privacy-sensitive preference, but if the app terminates before the change is durably written, the app will load the old value upon restart. Furthermore, the lack of transactional semantics makes operations like incrementing a counter unsafe without external locking.
- Data Safety: Data corruption can lead to silent failures or uncatchable exceptions. Additionally, writing malformed UTF-16 strings (such as unpaired surrogates) can result in silent data corruption. Modifying the collections returned by methods like
getStringSet(String,Set)can also lead to unexpected behavior and crashes.
Note: This class does not support use across multiple processes.
Summary
| Nested classes | |
|---|---|
| abstract |
Interface used for modifying values in a |
| abstract |
Interface definition for a callback to be invoked when a shared preference is changed. |
| Public methods | |
|---|---|
| abstract Boolean |
Checks whether the preferences contains a preference. |
| abstract SharedPreferences.Editor! |
edit()Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object. |
| abstract MutableMap<String!, *>! |
getAll()Retrieve all values from the preferences. |
| abstract Boolean |
getBoolean(: String!, : Boolean)Retrieve a boolean value from the preferences. |
| abstract Float |
Retrieve a float value from the preferences. |
| abstract Int |
Retrieve an int value from the preferences. |
| abstract Long |
Retrieve a long value from the preferences. |
| abstract String? |
Retrieve a String value from the preferences. |
| abstract MutableSet<String!>? |
getStringSet(: String!, : MutableSet<String!>?)Retrieve a set of String values from the preferences. |
| abstract Unit |
Registers a callback to be invoked when a change happens to a preference. |
| abstract Unit |
Unregisters a previous callback. |
Public methods
contains
abstract fun contains(: String!): Boolean
Checks whether the preferences contains a preference.
| Parameters | |
|---|---|
key |
String!: The name of the preference to check. |
| Return | |
|---|---|
Boolean |
Returns true if the preference exists in the preferences, otherwise false. |
edit
abstract fun edit(): SharedPreferences.Editor!
Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.
Note that you must call Editor.commit to have any changes you perform in the Editor actually show up in the SharedPreferences.
| Return | |
|---|---|
SharedPreferences.Editor! |
Returns a new instance of the Editor interface, allowing you to modify the values in this SharedPreferences object. |
getAll
abstract fun getAll(): MutableMap<String!, *>!
Retrieve all values from the preferences.
Note that you must not modify the collection returned by this method, or alter any of its contents. The consistency of your stored data is not guaranteed if you do.
| Return | |
|---|---|
MutableMap<String!, *>! |
Returns a map containing a list of pairs key/value representing the preferences. |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
|
getBoolean
abstract fun getBoolean(
: String!,
: Boolean
): Boolean
Retrieve a boolean value from the preferences.
| Parameters | |
|---|---|
key |
String!: The name of the preference to retrieve. |
defValue |
Boolean: Value to return if this preference does not exist. |
| Return | |
|---|---|
Boolean |
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a boolean. |
| Exceptions | |
|---|---|
java.lang.ClassCastException |
|
getFloat
abstract fun getFloat(
: String!,
: Float
): Float
Retrieve a float value from the preferences.
| Parameters | |
|---|---|
key |
String!: The name of the preference to retrieve. |
defValue |
Float: Value to return if this preference does not exist. |
| Return | |
|---|---|
Float |
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a float. |
| Exceptions | |
|---|---|
java.lang.ClassCastException |
|
getInt
abstract fun getInt(
: String!,
: Int
): Int
Retrieve an int value from the preferences.
| Parameters | |
|---|---|
key |
String!: The name of the preference to retrieve. |
defValue |
Int: Value to return if this preference does not exist. |
| Return | |
|---|---|
Int |
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int. |
| Exceptions | |
|---|---|
java.lang.ClassCastException |
|
getLong
abstract fun getLong(
: String!,
: Long
): Long
Retrieve a long value from the preferences.
| Parameters | |
|---|---|
key |
String!: The name of the preference to retrieve. |
defValue |
Long: Value to return if this preference does not exist. |
| Return | |
|---|---|
Long |
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a long. |
| Exceptions | |
|---|---|
java.lang.ClassCastException |
|
getString
abstract fun getString(
: String!,
: String?
): String?
Retrieve a String value from the preferences.
| Parameters | |
|---|---|
key |
String!: The name of the preference to retrieve. |
defValue |
String?: Value to return if this preference does not exist. This value may be null. |
| Return | |
|---|---|
String? |
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String. This value may be null. |
| Exceptions | |
|---|---|
java.lang.ClassCastException |
|
getStringSet
abstract fun getStringSet(
: String!,
: MutableSet<String!>?
): MutableSet<String!>?
Retrieve a set of String values from the preferences.
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.
| Parameters | |
|---|---|
key |
String!: The name of the preference to retrieve. |
defValues |
MutableSet<String!>?: Values to return if this preference does not exist. This value may be null. |
| Return | |
|---|---|
MutableSet<String!>? |
Returns the preference values if they exist, or defValues. Throws ClassCastException if there is a preference with this name that is not a Set. This value may be null. |
| Exceptions | |
|---|---|
java.lang.ClassCastException |
|
registerOnSharedPreferenceChangeListener
abstract fun registerOnSharedPreferenceChangeListener(: SharedPreferences.OnSharedPreferenceChangeListener!): Unit
Registers a callback to be invoked when a change happens to a preference.
Caution: The preference manager does not currently store a strong reference to the listener. You must store a strong reference to the listener, or it will be susceptible to garbage collection. We recommend you keep a reference to the listener in the instance data of an object that will exist as long as you need the listener.
| Parameters | |
|---|---|
listener |
SharedPreferences.OnSharedPreferenceChangeListener!: The callback that will run. |
unregisterOnSharedPreferenceChangeListener
abstract fun unregisterOnSharedPreferenceChangeListener(: SharedPreferences.OnSharedPreferenceChangeListener!): Unit
Unregisters a previous callback.
| Parameters | |
|---|---|
listener |
SharedPreferences.OnSharedPreferenceChangeListener!: The callback that should be unregistered. |