Google is committed to advancing racial equity for Black communities. See how.

Preferences

abstract class Preferences
kotlin.Any
   ↳ androidx.datastore.preferences.Preferences

Preferences and MutablePreferences are a lot like a generic Map and MutableMap keyed by the Preferences.Key class. These are intended for use with DataStore. Construct a DataStore instance using PreferenceDataStoreFactory.create.

Summary

Nested classes

Key for values stored in Preferences.

Key Value pairs for Preferences.

Public methods
abstract Map<Preferences.Key<*>, Any>

Retrieve a map of all key preference pairs.

abstract operator Boolean

abstract operator T?

Get a preference with a key.

Extension functions
From androidx.datastore.preferences
MutablePreferences

Gets a mutable copy of Preferences which contains all the preferences in this Preferences.

Preferences

Gets a read-only copy of Preferences which contains all the preferences in this Preferences.

Public methods

asMap

abstract fun asMap(): Map<Preferences.Key<*>, Any>

Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.

Return
a map containing all the preferences in this Preferences

contains

abstract operator fun <T> contains(key: Preferences.Key<T>): Boolean

get

abstract operator fun <T> get(key: Preferences.Key<T>): T?

Get a preference with a key. If the key is not set, returns null.

If T is Set, this returns an unmodifiable set which will throw a runtime exception when mutated. Do not try to mutate the returned set.

Use MutablePreferences.set to change the value of a preference (inside a DataStore.edit block).

Parameters
T the type of the preference
key: Preferences.Key<T> the key for the preference
Exceptions
ClassCastException if there is something stored with the same name as key but it cannot be cast to T