Android 12 Developer Preview is here! Try it out, and give us your feedback!

MutablePreferences

class MutablePreferences : Preferences
kotlin.Any
   ↳ androidx.datastore.preferences.core.Preferences
   ↳ androidx.datastore.preferences.core.MutablePreferences

Mutable version of Preferences. Allows for creating Preferences with different key-value pairs.

Summary

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

Retrieve a map of all key preference pairs.

Unit

operator Boolean

Returns true if this Preferences contains the specified key.

Boolean
equals(other: Any?)

operator T?

Get a preference with a key.

Int

operator Unit

Removes the preference with the given key from this MutablePreferences.

operator Unit

Appends or replaces all pairs from prefs to this MutablePreferences.

operator Unit

Appends or replaces all pair to this MutablePreferences.

Unit
putAll(vararg pairs: Preferences.Pair<*>)

Appends or replaces all pairs to this MutablePreferences.

T

Remove a preferences from this MutablePreferences.

operator Unit
set(key: Preferences.Key<T>, value: T)

Set a key value pair in MutablePreferences.

String

For better debugging.

Inherited functions

Public methods

asMap

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

clear

fun clear(): Unit

contains

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

Returns true if this Preferences contains the specified key.

Parameters
key: Preferences.Key<T> the key to check for

equals

fun equals(other: Any?): Boolean

get

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

hashCode

fun hashCode(): Int

minusAssign

operator fun minusAssign(key: Preferences.Key<*><