Google is committed to advancing racial equity for Black communities. See how.

UiSavedStateRegistry

interface UiSavedStateRegistry
androidx.compose.runtime.savedinstancestate.UiSavedStateRegistry

Allows components to save and restore their state using the saved instance state mechanism.

Summary

Public methods
abstract Boolean
canBeSaved(value: Any)

Returns true if the value can be saved using this Registry.

abstract Any?

Returns the restored value for the given key.

abstract Map<String, List<Any?>>

Executes all the registered value providers and combines these values into a map.

abstract Unit
registerProvider(key: String, valueProvider: () -> Any?)

Registers the value provider.

abstract Unit
unregisterProvider(key: String, valueProvider: () -> Any?)

Unregisters the value provider previously registered via registerProvider.

Public methods

canBeSaved

abstract fun canBeSaved(value: Any): Boolean

Returns true if the value can be saved using this Registry. The default implementation will return true if this value can be stored in Bundle.

Parameters
value: Any The value which we want to save using this Registry

consumeRestored

abstract fun consumeRestored(key: String): Any?

Returns the restored value for the given key. Once being restored the value is cleared, so you can't restore the same key twice.

Parameters
key: String Key used to save the value

performSave

abstract fun performSave(): Map<String, List<Any?>>

Executes all the registered value providers and combines these values into a map. We have a list of values for each key as it is allowed to have multiple providers for the same key.

registerProvider

abstract fun registerProvider(
    key: String,
    valueProvider: () -> Any?
): Unit

Registers the value provider.

There are could be multiple providers registered for the same key. In this case the order in which they were registered matters.

Say we registered two providers for the key. One provides "1", second provides "2". performSave in this case will have listOf("1", "2) as a value for the key in the map. And later, when the registry will be recreated with the previously saved values, the first execution of consumeRestored would consume "1" and the second one "2".

Parameters
key: String Key to use for storing the value
valueProvider: () -> Any? Provides the current value, to be executed when performSave will be triggered to collect all the registered values

unregisterProvider

abstract fun unregisterProvider(
    key: String,
    valueProvider: () -> Any?
): Unit

Unregisters the value provider previously registered via registerProvider.

Parameters
key: String Key of the value which shouldn't be saved anymore
valueProvider: () -> Any? The provider previously passed to registerProvider