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 |
Float |
Retrieves a |
Int |
Retrieves an |
Long |
Retrieves a |
String? |
Retrieves a |
(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 |
Unit |
Sets a |
Unit |
Sets an |
Unit |
Sets a |
Unit |
Sets a |
Unit |
putStringSet(key: String!, values: (Mutable)Set<String!>?)Sets a set of |
Public constructors
Public functions
getBoolean
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
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
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
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
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
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 |
| See also | |
|---|---|
putStringSet |
putBoolean
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
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
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
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
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
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 |