MutablePreferences
public final class MutablePreferences extends Preferences
| java.lang.Object | ||
| ↳ | 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 |
|
|---|---|
@NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object> |
asMap()Retrieve a map of all key preference pairs. |
final void |
clear() |
boolean |
<T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)Returns true if this Preferences contains the specified key. |
boolean |
|
T |
<T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)Get a preference with a key. |
int |
hashCode() |
final void |
minusAssign(@NonNull Preferences.Key<@NonNull ?> key)Removes the preference with the given key from this MutablePreferences. |
final void |
plusAssign(@NonNull Preferences.Pair<@NonNull ?> pair)Appends or replaces all |
final void |
plusAssign(@NonNull Preferences prefs)Appends or replaces all pairs from |
final void |
putAll(@NonNull Preferences.Pair<@NonNull ?>... pairs)Appends or replaces all |
final @NonNull T |
<T extends Object> remove(@NonNull Preferences.Key<@NonNull T> key)Remove a preferences from this MutablePreferences. |
final void |
Set a key value pair in MutablePreferences. |
@NonNull String |
toString()For better debugging. |
Inherited methods |
||||
|---|---|---|---|---|
|
Public methods
asMap
public @NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object> asMap()
Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.
contains
public boolean <T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)
Returns true if this Preferences contains the specified key.
| Parameters | |
|---|---|
@NonNull Preferences.Key<@NonNull T> key |
the key to check for |
get
public T <T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)
Get a preference with a key. If the key is not set, returns null.
If T is Set
Use MutablePreferences.set to change the value of a preference (inside a DataStore block).
| Parameters | |
|---|---|
<T extends Object> |
the type of the preference |
@NonNull Preferences.Key<@NonNull T> key |
the key for the preference |
| Throws | |
|---|---|
kotlin.ClassCastException |
if there is something stored with the same name as |
minusAssign
public final void minusAssign(@NonNull Preferences.Key<@NonNull ?> key)
Removes the preference with the given key from this MutablePreferences. If this Preferences does not contain the key, this is a no-op.
Example usage: mutablePrefs -= COUNTER_KEY
| Parameters | |
|---|---|
@NonNull Preferences.Key<@NonNull ?> key |
the key to remove from this MutablePreferences |
plusAssign
public final void plusAssign(@NonNull Preferences.Pair<@NonNull ?> pair)
Appends or replaces all pair to this MutablePreferences.
Example usage: mutablePrefs += COUNTER_KEY to 100
| Parameters | |
|---|---|
@NonNull Preferences.Pair<@NonNull ?> pair |
the Preference.Pair to add to this MutablePreferences |
plusAssign
public final void plusAssign(@NonNull Preferences prefs)
Appends or replaces all pairs from prefs to this MutablePreferences. Keys in prefs will overwrite keys in this Preferences.
Example usage: mutablePrefs += preferencesOf(COUNTER_KEY to 100, NAME to "abcdef")
| Parameters | |
|---|---|
@NonNull Preferences prefs |
Preferences to append to this MutablePreferences |
putAll
public final void putAll(@NonNull Preferences.Pair<@NonNull ?>... pairs)
Appends or replaces all pairs to this MutablePreferences.
| Parameters | |
|---|---|
@NonNull Preferences.Pair<@NonNull ?>... pairs |
the pairs to append to this MutablePreferences |
remove
public final @NonNull T <T extends Object> remove(@NonNull Preferences.Key<@NonNull T> key)
Remove a preferences from this MutablePreferences.
| Parameters | |
|---|---|
@NonNull Preferences.Key<@NonNull T> key |
the key to remove this MutablePreferences |
| Returns | |
|---|---|
@NonNull T |
the original value of this preference key. |
set
public final void <T extends Object> set(@NonNull Preferences.Key<@NonNull T> key, @NonNull T value)
Set a key value pair in MutablePreferences.
Example usage: val COUNTER_KEY = intPreferencesKey("counter")
// Once edit completes successfully, preferenceStore will contain the incremented counter. preferenceStore.edit { prefs: MutablePreferences -> prefs\[COUNTER_KEY\] = prefs\[COUNTER_KEY\] :? 0 + 1 }
| Parameters | |
|---|---|
@NonNull Preferences.Key<@NonNull T> key |
the preference to set |
@NonNull T value |
the value to set the preference to |