androidx.savedstate


Interfaces

SavedStateRegistry.AutoRecreated

Subclasses of this interface will be automatically recreated if they were previously registered via runOnNextRecreation.

android
SavedStateRegistry.SavedStateProvider

This interface marks a component that contributes to saved state.

Cmn
android
SavedStateRegistryOwner

A scope that owns SavedStateRegistry

Cmn

Classes

SavedState

An opaque (empty) common type that holds saveable values to be saved and restored by native platforms that have a concept of System-initiated Process Death.

Cmn
android
SavedStateReader

An inline class that encapsulates an opaque SavedState, and provides an API for reading the platform specific state.

Cmn
android
SavedStateRegistry

An interface for plugging components that consumes and contributes to the saved state.

Cmn
android
SavedStateRegistryController

An API for SavedStateRegistryOwner implementations to control SavedStateRegistry.

Cmn
android
SavedStateWriter

An inline class that encapsulates an opaque SavedState, and provides an API for writing the platform specific state.

Cmn
android

Type aliases

SavedState
android

Top-level functions summary

inline SavedState
savedState(initialState: Map<StringAny?>, builderAction: SavedStateWriter.() -> Unit)

Builds a new SavedState with the specified initialState, given as a Map of String keys and Any value.

Cmn
android

Extension functions summary

SavedStateRegistryOwner?

Retrieve the SavedStateRegistryOwner responsible for managing the saved state for this View.

android
inline T
<T : Any?> SavedState.read(block: SavedStateReader.() -> T)

Calls the specified function block with a SavedStateReader value as its receiver and returns the block value.

Cmn
Unit

Set the SavedStateRegistryOwner responsible for managing the saved state for this View Calls to get from this view or descendants will return owner.

android
inline T
<T : Any?> SavedState.write(block: SavedStateWriter.() -> T)

Calls the specified function block with a SavedStateWriter value as its receiver and returns the block value.

Cmn

Top-level functions

savedState

inline fun savedState(
    initialState: Map<StringAny?> = emptyMap(),
    builderAction: SavedStateWriter.() -> Unit = {}
): SavedState

Builds a new SavedState with the specified initialState, given as a Map of String keys and Any value.

Allows further modification of the state using the builderAction.

IMPORTANT: The SavedStateWriter passed as a receiver to the builderAction is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
initialState: Map<StringAny?> = emptyMap()

An initial map of key-value pairs to populate the state. Defaults to an empty map.

builderAction: SavedStateWriter.() -> Unit = {}

A lambda function with a SavedStateWriter receiver to modify the state.

Returns
SavedState

A SavedState instance containing the initialized key-value pairs.

Extension functions

findViewTreeSavedStateRegistryOwner

fun View.findViewTreeSavedStateRegistryOwner(): SavedStateRegistryOwner?

Retrieve the SavedStateRegistryOwner responsible for managing the saved state for this View. This may be used to save or restore the state associated with the view.

The returned SavedStateRegistryOwner is managing all the Views within the Fragment or Activity this View is added to.

Returns
SavedStateRegistryOwner?

The SavedStateRegistryOwner responsible for managing the saved state for this view and/or some subset of its ancestors

inline fun <T : Any?> SavedState.read(block: SavedStateReader.() -> T): T

Calls the specified function block with a SavedStateReader value as its receiver and returns the block value.

IMPORTANT: The SavedStateReader passed as a receiver to the block is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
block: SavedStateReader.() -> T

A lambda function that performs read operations using the SavedStateReader.

Returns
T

The result of the lambda function's execution.

setViewTreeSavedStateRegistryOwner

fun View.setViewTreeSavedStateRegistryOwner(
    owner: SavedStateRegistryOwner?
): Unit

Set the SavedStateRegistryOwner responsible for managing the saved state for this View Calls to get from this view or descendants will return owner.

This is is automatically set for you in the common cases of using fragments or ComponentActivity.

This should only be called by constructs such as activities or fragments that manage a view tree and their saved state through a SavedStateRegistryOwner. Callers should only set a SavedStateRegistryOwner that will be stable. The associated SavedStateRegistry should be cleared if the view tree is removed and is not guaranteed to later become reattached to a window.

Parameters
owner: SavedStateRegistryOwner?

The SavedStateRegistryOwner responsible for managing the saved state for the given view

inline fun <T : Any?> SavedState.write(block: SavedStateWriter.() -> T): T

Calls the specified function block with a SavedStateWriter value as its receiver and returns the block value.

IMPORTANT: The SavedStateWriter passed as a receiver to the block is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
block: SavedStateWriter.() -> T

A lambda function that performs write operations using the SavedStateWriter.

Returns
T

The result of the lambda function's execution.