PreferenceDataStore
abstract class PreferenceDataStore
kotlin.Any | |
↳ | androidx.preference.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 | |
---|---|
<init>() A data store interface to be implemented and provided to the |
Public methods | |
---|---|
open Boolean |
getBoolean(key: String!, defValue: Boolean) Retrieves a |
open Float |
Retrieves a |
open Int |
Retrieves an |
open Long |
Retrieves a |
open String? |
Retrieves a |
open MutableSet<String!>? |
getStringSet(key: String!, @Nullable defValues: MutableSet<String!>?) Retrieves a set of Strings from the data store. |
open Unit |
putBoolean(key: String!, value: Boolean) Sets a |
open Unit |
Sets a |
open Unit |
Sets an |
open Unit |
Sets a |
open Unit |
Sets a |
open Unit |
putStringSet(key: String!, @Nullable values: MutableSet<String!>?) Sets a set of |
Public constructors
<init>
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
.
Public methods
getBoolean
open 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 |
Return | |
---|---|
Boolean |
the value from the data store or the default return value |
See Also
getFloat
open fun getFloat(
&n