PreferenceDataStore

abstract class PreferenceDataStore(source)

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.

See also

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
open fun getBoolean(@NonNull key: String, defValue: Boolean): Boolean
Retrieves a Boolean value from the data store.
Link copied to clipboard
open fun getFloat(@NonNull key: String, defValue: Float): Float
Retrieves a Float value from the data store.
Link copied to clipboard
open fun getInt(@NonNull key: String, defValue: Int): Int
Retrieves an Integer value from the data store.
Link copied to clipboard
open fun getLong(@NonNull key: String, defValue: Long): Long
Retrieves a Long value from the data store.
Link copied to clipboard
open fun getString(@NonNull key: String, @Nullable defValue: String): String
Retrieves a String value from the data store.
Link copied to clipboard
open fun getStringSet(@NonNull key: String, @Nullable defValues: Set<String>): Set<String>
Retrieves a set of Strings from the data store.
Link copied to clipboard
open fun putBoolean(@NonNull key: String, value: Boolean)
Sets a Boolean value to the data store.
Link copied to clipboard
open fun putFloat(@NonNull key: String, value: Float)
Sets a Float value to the data store.
Link copied to clipboard
open fun putInt(@NonNull key: String, value: Int)
Sets an Integer value to the data store.
Link copied to clipboard
open fun putLong(@NonNull key: String, value: Long)
Sets a Long value to the data store.
Link copied to clipboard
open fun putString(@NonNull key: String, @Nullable value: String)
Sets a String value to the data store.
Link copied to clipboard
open fun putStringSet(@NonNull key: String, @Nullable values: Set<String>)
Sets a set of Strings to the data store.