PreferenceDataStore


abstract class PreferenceDataStore


A data store interface to be implemented and provided to the Preference 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 database, 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 is not supposed to be stored at all because they are only valid per session.

Once a put method is called it is the 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 constructors

Public functions

Boolean
getBoolean(key: String!, defValue: Boolean)

Retrieves a Boolean value from the data store.

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

Retrieves a Float value from the data store.

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

Retrieves an Integer value from the data store.

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

Retrieves a Long value from the data store.

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

Retrieves a String value from the data store.

(Mutable)Set<String!>?
getStringSet(key: String!, defValues: (Mutable)Set<String!>?)

Retrieves a set of Strings from the data store.

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

Sets a Boolean value to the data store.

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

Sets a Float value to the data store.

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

Sets an Integer value to the data store.

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

Sets a Long value to the data store.

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

Sets a String value to the data store.

Unit
putStringSet(key: String!, values: (Mutable)Set<String!>?)

Sets a set of Strings to the data store.

Public constructors

PreferenceDataStore

Added in 1.0.0
PreferenceDataStore()

Public functions

getBoolean

Added in 1.0.0
fun getBoolean(key: String!, defValue: Boolean): Boolean

Retrieves 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 in the storage

Returns
Boolean

the value from the data store or the default return value

See also
getBoolean

getFloat

Added in 1.0.0
fun getFloat(key: String!, defValue: Float): Float

Retrieves 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 in the storage

Returns
Float

The value from the data store or the default return value

See also
putFloat

getInt

Added in 1.0.0
fun getInt(key: String!, defValue: Int): Int

Retrieves an Integer 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 in the storage

Returns
Int

The value from the data store or the default return value

See also
putInt

getLong

Added in 1.0.0
fun getLong(key: String!, defValue: Long): Long

Retrieves 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 in the storage

Returns
Long

The value from the data store or the default return value

See also
putLong

getString

Added in 1.0.0
fun getString(key: String!, defValue: String?): String?

Retrieves 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 in the storage

Returns
String?

The value from the data store or the default return value

See also
putString

getStringSet

Added in 1.0.0
fun getStringSet(key: String!, defValues: (Mutable)Set<String!>?): (Mutable)Set<String!>?

Retrieves a set of Strings from the data store.

Parameters
key: String!

The name of the preference to retrieve

defValues: (Mutable)Set<String!>?

Values to return if this preference does not exist in the storage

Returns
(Mutable)Set<String!>?

The values from the data store or the default return values

See also
putStringSet

putBoolean

Added in 1.0.0
fun putBoolean(key: String!, value: Boolean): Unit

Sets 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

See also
getBoolean

putFloat

Added in 1.0.0
fun putFloat(key: String!, value: Float): Unit

Sets 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

See also
getFloat

putInt

Added in 1.0.0
fun putInt(key: String!, value: Int): Unit

Sets an Integer 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

See also
getInt

putLong

Added in 1.0.0
fun putLong(key: String!, value: Long): Unit

Sets 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

See also
getLong

putString

Added in 1.0.0
fun putString(key: String!, value: String?): Unit

Sets 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

See also
getString

putStringSet

Added in 1.0.0
fun putStringSet(key: String!, values: (Mutable)Set<String!>?): Unit

Sets a set of Strings 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: (Mutable)Set<String!>?

The set of new values for the preference

See also
getStringSet