Added in API level 26
Deprecated in API level 29

PreferenceDataStore


interface PreferenceDataStore
android.preference.PreferenceDataStore

A data store interface to be implemented and provided to the Preferences framework. This can be used to replace the default android.content.SharedPreferences, if needed.

In most cases you want to use android.content.SharedPreferences as it is automatically backed up and migrated to new devices. However, providing custom data store to preferences can be useful if your app stores its preferences in a local db, cloud or they are device specific like "Developer settings". It might be also useful when you want to use the preferences UI but the data are not supposed to be stored at all because they are valid per session only.

Once a put method is called it is full responsibility of the data store implementation to safely store the given values. Time expensive operations need to be done in the background to prevent from blocking the UI. You also need to have a plan on how to serialize the data in case the activity holding this object gets destroyed.

By default, all "put" methods throw UnsupportedOperationException.

Summary

Public methods
open Boolean
getBoolean(key: String!, defValue: Boolean)

Retrieve a boolean value from the data store.

open Float
getFloat(key: String!, defValue: Float)

Retrieve a float value from the data store.

open Int
getInt(key: String!, defValue: Int)

Retrieve an int value from the data store.

open Long
getLong(key: String!, defValue: Long)

Retrieve a long value from the data store.

open String?
getString(key: String!, defValue: String?)

Retrieve a String value from the data store.

open MutableSet<String!>?
getStringSet(key: String!, defValues: MutableSet<String!>?)

Retrieve a set of String values from the data store.

open Unit
putBoolean(key: String!, value: Boolean)

Set a boolean value to the data store.

open Unit
putFloat(key: String!, value: Float)

Set a float value to the data store.

open Unit
putInt(key: String!, value: Int)

Set an int value to the data store.

open Unit
putLong(key: String!, value: Long)

Set a long value to the data store.

open Unit
putString(key: String!, value: String?)

Set a String value to the data store.

open Unit
putStringSet(key: String!, values: MutableSet<String!>?)

Set a set of String value to the data store.

Public methods

getBoolean

Added in API level 26
open fun getBoolean(
    key: String!,
    defValue: Boolean
): Boolean

Deprecated: Deprecated in Java.

Retrieve a boolean value from the data store.

Parameters
key String!: The name of the preference to retrieve.
defValue Boolean: Value to return if this preference does not exist.

getFloat

Added in API level 26
open fun getFloat(
    key: String!,
    defValue: Float
): Float

Deprecated: Deprecated in Java.

Retrieve a float value from the data store.

Parameters
key String!: The name of the preference to retrieve.
defValue Float: Value to return if this preference does not exist.

getInt

Added in API level 26
open fun getInt(
    key: String!,
    defValue: Int
): Int

Deprecated: Deprecated in Java.

Retrieve an int value from the data store.

Parameters
key String!: The name of the preference to retrieve.
defValue Int: Value to return if this preference does not exist.

getLong

Added in API level 26
open fun getLong(
    key: String!,
    defValue: Long
): Long

Deprecated: Deprecated in Java.

Retrieve a long value from the data store.

Parameters
key String!: The name of the preference to retrieve.
defValue Long: Value to return if this preference does not exist.

getString

Added in API level 26
open fun getString(
    key: String!,
    defValue: String?
): String?

Deprecated: Deprecated in Java.

Retrieve a String value from the data store.

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? This value may be null.

getStringSet

Added in API level 26
open fun getStringSet(
    key: String!,
    defValues: MutableSet<String!>?
): MutableSet<String!>?

Deprecated: Deprecated in Java.

Retrieve a set of String values from the data store.

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!>? This value may be null.

putBoolean

Added in API level 26
open fun putBoolean(
    key: String!,
    value: Boolean
): Unit

Deprecated: Deprecated in Java.

Set a boolean value to the data store.

Once the value is set the data store is responsible for holding it.

Parameters
key String!: The name of the preference to modify.
value Boolean: The new value for the preference.

putFloat

Added in API level 26
open fun putFloat(
    key: String!,
    value: Float
): Unit

Deprecated: Deprecated in Java.

Set a float value to the data store.

Once the value is set the data store is responsible for holding it.

Parameters
key String!: The name of the preference to modify.
value Float: The new value for the preference.

putInt

Added in API level 26
open fun putInt(
    key: String!,
    value: Int
): Unit

Deprecated: Deprecated in Java.

Set an int value to the data store.

Once the value is set the data store is responsible for holding it.

Parameters
key String!: The name of the preference to modify.
value Int: The new value for the preference.

putLong

Added in API level 26
open fun putLong(
    key: String!,
    value: Long
): Unit

Deprecated: Deprecated in Java.

Set a long value to the data store.

Once the value is set the data store is responsible for holding it.

Parameters
key String!: The name of the preference to modify.
value Long: The new value for the preference.

putString

Added in API level 26
open fun putString(
    key: String!,
    value: String?
): Unit

Deprecated: Deprecated in Java.

Set a String value to the data store.

Once the value is set the data store is responsible for holding it.

Parameters
key String!: The name of the preference to modify.
value String?: The new value for the preference. This value may be null.

putStringSet

Added in API level 26
open fun putStringSet(
    key: String!,
    values: MutableSet<String!>?
): Unit

Deprecated: Deprecated in Java.

Set a set of String value to the data store.

Once the value is set the data store is responsible for holding it.

Parameters
key String!: The name of the preference to modify.
values MutableSet<String!>?: The set of new values for the preference. This value may be null.