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 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 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

void clearSavedStateProvider(String key)

Clear any SavedStateRegistry.SavedStateProvider that was previously set via setSavedStateProvider(String, SavedStateProvider).

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 LiveData that access data associated with the given key.

<T> MutableLiveData<T> getLiveData(String key, T initialValue)

Returns a LiveData that access data associated with the given key.

Set<String> keys()

Returns all keys contained in this SavedStateHandle

Returned set contains all keys: keys used to get LiveData-s, to set SavedStateProviders and keys used in regular set(String, Object).

<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.

void setSavedStateProvider(String key,