SaveableStateRegistry


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

Summary

Nested types

The registry entry which you get when you use registerProvider.

Public functions

Boolean
canBeSaved(value: Any)

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

Cmn
Any?

Returns the restored value for the given key.

Cmn
Map<StringList<Any?>>

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

Cmn
SaveableStateRegistry.Entry
registerProvider(key: String, valueProvider: () -> Any?)

Registers the value provider.

Cmn

Public functions

canBeSaved

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

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

fun performSave(): Map<StringList<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

fun registerProvider(key: String, valueProvider: () -> Any?): SaveableStateRegistry.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
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

Returns
SaveableStateRegistry.Entry

the registry entry which you can use to unregister the provider