SaveableStateRegistry

Interface

Common
public interface SaveableStateRegistry

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

Functions

public 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

keyKey used to save the value
public fun registerProvider(key: String, valueProvider: () -> Any?): Entry

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

keyKey to use for storing the value
valueProviderProvides the current value, to be executed when performSave will be triggered to collect all the registered values

Returns

the registry entry which you can use to unregister the provider
public 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

valueThe value which we want to save using this Registry
public 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.