SavedStateContainer



A container that manages SavedStateValue instances to coordinate state saving and restoration across hierarchical and key-value based components.

SavedStateContainer allows registering, retrieving, removing, and nesting state-saving components. When saving or restoring state, operations are delegated across all registered SavedStateValue instances.

Summary

Public constructors

Cmn

Public functions

operator Boolean

Checks if a SavedStateValue is registered under the given key.

Cmn
operator SavedStateValue<T>?
<T : Any?> get(key: String)

Retrieves the registered SavedStateValue associated with key.

Cmn
SavedStateContainer

Retrieves an existing nested SavedStateContainer associated with key, or creates and registers a new child SavedStateContainer if none exists.

Cmn
R
<T : Any?, R : SavedStateValue<T>> getOrPut(key: String, defaultValue: () -> R)

Returns an existing SavedStateValue registered for key, or evaluates defaultValue, registers the result in this container, and returns it.

Cmn
Set<String>

Returns a Set containing all keys registered in this SavedStateContainer.

Cmn
SavedStateValue<T>?
<T : Any?> put(key: String, value: SavedStateValue<T>)

Registers a value for state saving and restoration under the given key.

Cmn
SavedStateValue<T>?
<T : Any?> remove(key: String)

Removes and returns the registered SavedStateValue associated with key.

Cmn
Unit
restoreState(savedState: SavedState?)

Restores state for all registered SavedStateValue instances using the provided savedState.

Cmn
SavedState

Invokes state saving on all registered SavedStateValue instances and bundles their states into a single SavedState.

Cmn
operator Unit
<T : Any?> set(key: String, value: SavedStateValue<T>)

Registers a value for state saving and restoration under the given key.

Cmn

Public constructors

SavedStateContainer

SavedStateContainer()

Public functions

contains

operator fun contains(key: String): Boolean

Checks if a SavedStateValue is registered under the given key.

Parameters
key: String

unique identifier for the saved state value

Returns
Boolean

true if a SavedStateValue is associated with key, false otherwise

get

operator fun <T : Any?> get(key: String): SavedStateValue<T>?

Retrieves the registered SavedStateValue associated with key.

Parameters
key: String

unique identifier for the saved state value

Returns
SavedStateValue<T>?

registered SavedStateValue, or null if no value is found

getOrCreateContainer

fun getOrCreateContainer(key: String): SavedStateContainer

Retrieves an existing nested SavedStateContainer associated with key, or creates and registers a new child SavedStateContainer if none exists.

Parameters
key: String

unique identifier for the child container

Returns
SavedStateContainer

existing or newly created child SavedStateContainer instance

getOrPut

fun <T : Any?, R : SavedStateValue<T>> getOrPut(key: String, defaultValue: () -> R): R

Returns an existing SavedStateValue registered for key, or evaluates defaultValue, registers the result in this container, and returns it.

Parameters
key: String

unique identifier for the saved state value

defaultValue: () -> R

factory producing a new SavedStateValue if none is registered for key

Returns
R

existing or newly registered SavedStateValue instance

Throws
ClassCastException

if an existing SavedStateValue registered for key is not an instance of R

keys

fun keys(): Set<String>

Returns a Set containing all keys registered in this SavedStateContainer.

Returns
Set<String>

set of registered keys

put

fun <T : Any?> put(key: String, value: SavedStateValue<T>): SavedStateValue<T>?

Registers a value for state saving and restoration under the given key.

If a SavedStateValue was previously registered under key, it is overwritten and returned.

Parameters
key: String

unique identifier for the saved state value

value: SavedStateValue<T>

component responsible for state saving and restoration

Returns
SavedStateValue<T>?

previously registered SavedStateValue, or null if none was registered

remove

fun <T : Any?> remove(key: String): SavedStateValue<T>?

Removes and returns the registered SavedStateValue associated with key.

Parameters
key: String

unique identifier for the saved state value to remove

Returns
SavedStateValue<T>?

previously registered SavedStateValue, or null if none was registered

restoreState

fun restoreState(savedState: SavedState?): Unit

Restores state for all registered SavedStateValue instances using the provided savedState.

Parameters
savedState: SavedState?

previously saved state container, or null if no state was saved

saveState

fun saveState(): SavedState

Invokes state saving on all registered SavedStateValue instances and bundles their states into a single SavedState.

Returns
SavedState

a SavedState containing the collected saved states of all registered providers

set

operator fun <T : Any?> set(key: String, value: SavedStateValue<T>): Unit

Registers a value for state saving and restoration under the given key.

If a SavedStateValue was previously registered under key, it will be overwritten.

Parameters
key: String

unique identifier for the saved state value

value: SavedStateValue<T>

component responsible for state saving and restoration