SavedStateHandle
public
final
class
SavedStateHandle
extends Object
| java.lang.Object | |
| ↳ | androidx.lifecycle.SavedStateHandle |
A handle to saved state passed down to ViewModel. You should use
SavedStateVMFactory 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 LiveData returned
by getLiveData(String).
You can write a value to it via set(String, Object) or setting a value to
MutableLiveData returned by getLiveData(String).
Summary
Public constructors | |
|---|---|
SavedStateHandle(Map<String, Object> initialState)
Creates a handle with the given initial arguments. |
|
SavedStateHandle()
Creates a handle with the empty state. |
|
Public methods | |
|---|---|
boolean
|
contains(String key)
|
<T>
T
|
get(String key)
Returns a value associated with the given key. |
<T>
MutableLiveData<T>
|
getLiveData(String key)
Returns a |
Set<String>
|
keys()
Returns all keys contained in this |
<T>
T
|
remove(String key)
Removes a value associated with the given key. |
<T>
void
|
set(String key, T value)
Associate the given value with the key. |
Inherited methods | |
|---|---|
Public constructors
SavedStateHandle
public SavedStateHandle (Map<String, Object> initialState)
Creates a handle with the given initial arguments.
| Parameters | |
|---|---|
initialState |
Map |
SavedStateHandle
public SavedStateHandle ()
Creates a handle with the empty state.
Public methods
contains
public boolean contains (String key)
| Parameters | |
|---|---|
key |
String |
| Returns | |
|---|---|
boolean |
true if there is value associated with the given key. |
get
public T get (String key)
Returns a value associated with the given key.
| Parameters | |
|---|---|
key |
String |
| Returns | |
|---|---|
T |
|
getLiveData
public MutableLiveData<T> getLiveData (String key)
Returns a LiveData that access data associated with the given key,.
| Parameters | |
|---|---|
key |
String |
| Returns | |
|---|---|
MutableLiveData<T> |
|
keys
public Set<String> keys ()
Returns all keys contained in this SavedStateHandle
| Returns | |
|---|---|
Set<String> |
|
remove
public T remove (String key)
Removes a value associated with the given key. If there is a LiveData associated
with the given key, it will be removed as well.
All changes to LiveData previously
returned by getLiveData(String) won't be reflected in
the saved state. Also that LiveData won't receive any updates about new values
associated by the given key.
| Parameters | |
|---|---|
key |
String: a key |
| Returns | |
|---|---|
T |
a value that was previously associated with the given key. |
set
public void set (String key,
T value)
Associate the given value with the key. The value must have a type that could be stored in
Bundle
| Parameters | |
|---|---|
key |
String |
value |
T |
Annotations
Interfaces
Classes
- AbstractSavedStateVMFactory
- AndroidViewModel
- Lifecycle
- LifecycleRegistry
- LifecycleService
- LiveData
- LiveDataReactiveStreams
- MediatorLiveData
- MutableLiveData
- ProcessLifecycleOwner
- SavedStateHandle
- SavedStateVMFactory
- ServiceLifecycleDispatcher
- Transformations
- ViewModel
- ViewModelProvider
- ViewModelProvider.AndroidViewModelFactory
- ViewModelProvider.NewInstanceFactory
- ViewModelProviders
- ViewModelProviders.DefaultFactory
- ViewModelStore
- ViewModelStores
Enums