SavedStateHandle
class SavedStateHandle
kotlin.Any | |
↳ | androidx.lifecycle.SavedStateHandle |
A handle to saved state passed down to androidx.lifecycle.ViewModel
. You should use SavedStateViewModelFactory
if you want to receive this object in ViewModel
's constructor.
This is a key-value map that will let you write and retrieve objects to and from the saved state. These values will persist after the process is killed by the system and remain available via the same object.
You can read a value from it via get(String)
or observe it via androidx.lifecycle.LiveData
returned by getLiveData(String)
.
You can write a value to it via set(String, Object)
or setting a value to androidx.lifecycle.MutableLiveData
returned by getLiveData(String)
.
Summary
Public constructors | |
---|---|
<init>(@NonNull initialState: MutableMap<String!, Any!>) Creates a handle with the given initial arguments. |
|
<init>() Creates a handle with the empty state. |
Public methods | |
---|---|
Unit |
clearSavedStateProvider(@NonNull key: String) Clear any |
Boolean | |
T? |
Returns a value associated with the given key. |
MutableLiveData<T> |
getLiveData(@NonNull key: String) Returns a |
MutableLiveData<T> |
getLiveData(@NonNull key: String, initialValue: T) Returns a |
MutableSet<String!> |
keys() Returns all keys contained in this |
T? |
Removes a value associated with the given key. |
Unit |
Associate the given value with the key. |
Unit |
setSavedStateProvider(@NonNull key: String, @NonNull provider: SavedStateRegistry.SavedStateProvider) Set a |
Public constructors
<init>
SavedStateHandle(@NonNull initialState: MutableMap<String!, Any!>)
Creates a handle with the given initial arguments.
<init>
SavedStateHandle()
Creates a handle with the empty state.
Public methods
clearSavedStateProvider
@MainThread fun clearSavedStateProvider(@NonNull key: String): Unit
Clear any SavedStateProvider
that was previously set via setSavedStateProvider(String, SavedStateProvider)
. Note: calling this method within SavedStateProvider#saveState()
is supported, but will only affect future state saving operations.
Parameters | |
---|---|
key |
String: a key previously used with setSavedStateProvider |
contains
@MainThread fun contains(@NonNull key: String): Boolean
Return | |
---|---|
Boolean |
true if there is value associated with the given key. |
get
@MainThread @Nullable fun <T : Any!> get(@NonNull key: String): T?
Returns a value associated with the given key.
getLiveData
@MainThread @NonNull fun <T : Any!> getLiveData(@NonNull key: String): MutableLiveData<T>
Returns a androidx.lifecycle.LiveData
that access data associated with the given key.
See Also
getLiveData
@MainThread @NonNull fun <T : Any!> getLiveData(
@NonNull key: String,
initialValue: T
): MutableLiveData<T>
Returns a androidx.lifecycle.LiveData
that access