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
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>> getOrPutSavedStateValue(
    key: String,
    defaultValue: () -> R
)

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

Cmn
R?
<T : Any?, R : SavedStateValue<T>> getSavedStateValue(key: String)

Retrieves the registered SavedStateValue associated with key.

Cmn
Set<String>

Returns a Set containing all keys registered in this SavedStateContainer.

Cmn
Unit
<T : Any?, R : SavedStateValue<T>> putSavedStateValue(
    key: String,
    savedStateValue: R
)

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

Cmn
R?
<T : Any?, R : SavedStateValue<T>> removeSavedStateValue(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

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

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

getOrPutSavedStateValue

fun <T : Any?, R : SavedStateValue<T>> getOrPutSavedStateValue(
    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

getSavedStateValue

fun <T : Any?, R : SavedStateValue<T>> getSavedStateValue(key: String): R?

Retrieves the registered SavedStateValue associated with key.

Parameters
key: String

unique identifier for the saved state value

Returns
R?

registered SavedStateValue typed as R, or null if no value is found

keys

fun keys(): Set<String>

Returns a Set containing all keys registered in this SavedStateContainer.

Returns
Set<String>

set of registered keys

putSavedStateValue

fun <T : Any?, R : SavedStateValue<T>> putSavedStateValue(
    key: String,
    savedStateValue: R
): Unit

Registers a savedStateValue 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

savedStateValue: R

component responsible for state saving and restoration

removeSavedStateValue

fun <T : Any?, R : SavedStateValue<T>> removeSavedStateValue(key: String): R?

Removes and returns the registered SavedStateValue associated with key.

Parameters
key: String

unique identifier for the saved state value to remove

Returns
R?

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