SavedStateHandle

class SavedStateHandle
kotlin.Any
   ↳ 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

<init>(@NonNull initialState: MutableMap<String!, Any!>)

Creates a handle with the given initial arguments.

Creates a handle with the empty state.

Public methods

Boolean
contains(@NonNull key: String)

T?
get(@NonNull key: String)

Returns a value associated with the given key.

MutableLiveData<T>
getLiveData(@Nullable key: String?)

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

MutableSet<String!>

Returns all keys contained in this SavedStateHandle

T?
remove(@NonNull key: String)

Removes a value associated with the given key.

Unit
set(@NonNull key: String, @Nullable value: T?)

Associate the given value with the key.

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

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(@Nullable key: String?): MutableLiveData<T>

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

keys

@MainThread @NonNull fun keys(): MutableSet<String!>

Returns all keys contained in this SavedStateHandle

remove

@MainThread @Nullable fun <T : Any!> remove(@NonNull key: String): T?

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 SavedStateHandle#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
Return
T?: a value that was previously associated with the given key.

set

@MainThread fun <T : Any!> set(@NonNull key: String, @Nullable value: T?): Unit

Associate the given value with the key. The value must have a type that could be stored in android.os.Bundle

Parameters
<T> String: any type that can be accepted by Bundle.