SavedStateRegistry



Registry for components that consume and contribute to saved state.

Use this registry to save and restore component state across process death or recreation.

Summary

Nested types

This interface is deprecated. `AutoRecreated` is deprecated.

Contributes to the saved state.

Public constructors

Creates an empty SavedStateRegistry.

Cmn
android
SavedStateRegistry(initialState: Bundle)

Creates a SavedStateRegistry initialized with initialState.

android

Creates a SavedStateRegistry initialized with initialState.

Cmn

Public functions

SavedState?

Consumes the saved state previously supplied by a SavedStateProvider registered with the given key.

Cmn
android
SavedStateRegistry.SavedStateProvider?

Returns the SavedStateProvider previously registered with registerSavedStateProvider, or null if no provider has been registered with the given key.

Cmn
android
Unit

Registers a SavedStateProvider with the given key.

Cmn
android
open Unit
restoreState(savedState: Bundle?)

Called to restore the state of a component.

android
open Unit
restoreState(savedState: SavedState?)

Called to restore the state of a component.

Cmn
Unit

This function is deprecated. `runOnNextRecreation` is deprecated.

android
open SavedState

Called to retrieve the state from a component before it is killed so the state can be retrieved later from SavedStateRegistry.consumeRestoredStateForKey.

Cmn
android
Unit

Unregisters a component previously registered with the given key.

Cmn
android

Public properties

Boolean

Returns true if state has been restored and can be safely consumed with consumeRestoredStateForKey, false otherwise.

Cmn
android

Public constructors

SavedStateRegistry

SavedStateRegistry()

Creates an empty SavedStateRegistry.

SavedStateRegistry

SavedStateRegistry(initialState: Bundle)

Creates a SavedStateRegistry initialized with initialState.

Parameters
initialState: Bundle

The initial saved state to restore from.

SavedStateRegistry

SavedStateRegistry(initialState: SavedState)

Creates a SavedStateRegistry initialized with initialState.

Parameters
initialState: SavedState

The initial saved state to restore from.

Public functions

consumeRestoredStateForKey

@MainThread
fun consumeRestoredStateForKey(key: String): SavedState?

Consumes the saved state previously supplied by a SavedStateProvider registered with the given key.

If the registered SavedStateProvider implements SavedStateRestorer, the state is restored automatically during restoration, and calls to this method with the same key return null.

This call clears the internal reference to the returned saved state. Subsequent calls with the same key return null.

All unconsumed values are preserved during state saving.

Parameters
key: String

The key with which the SavedStateProvider was previously registered.

Returns
SavedState?

The previously saved state, or null if none exists or it has already been consumed.

getSavedStateProvider

fun getSavedStateProvider(key: String): SavedStateRegistry.SavedStateProvider?

Returns the SavedStateProvider previously registered with registerSavedStateProvider, or null if no provider has been registered with the given key.

Parameters
key: String

The key used to register the SavedStateProvider.

registerSavedStateProvider

@MainThread
fun registerSavedStateProvider(
    key: String,
    provider: SavedStateRegistry.SavedStateProvider
): Unit

Registers a SavedStateProvider with the given key.

This SavedStateProvider will be called during state saving. The returned state is associated with the given key and can be consumed after restoration via consumeRestoredStateForKey.

If the registered provider implements SavedStateRestorer, its SavedStateRestorer.restoreState method is automatically invoked during state restoration, or immediately if state has already been restored.

If a provider was already registered with the given key, it is replaced with the new provider.

Parameters
key: String

The key to associate with the provider.

provider: SavedStateRegistry.SavedStateProvider

The SavedStateProvider to register.

restoreState

open fun restoreState(savedState: Bundle?): Unit

Called to restore the state of a component.

Parameters
savedState: Bundle?

The SavedState containing the previously saved state, or null if no state was previously saved for this component.

restoreState

open fun restoreState(savedState: SavedState?): Unit

Called to restore the state of a component.

Parameters
savedState: SavedState?

The SavedState containing the previously saved state, or null if no state was previously saved for this component.

runOnNextRecreation

@MainThread
fun runOnNextRecreation(clazz: Class<SavedStateRegistry.AutoRecreated>): Unit

Executes the given class when the owning component restarted.

The given class will be automatically instantiated via default constructor and method AutoRecreated.onRecreated will be called. It is called as part of dispatching of androidx.lifecycle.Lifecycle.Event.ON_CREATE event.

Parameters
clazz: Class<SavedStateRegistry.AutoRecreated>

that will need to be instantiated on the next component recreation

Throws
IllegalArgumentException

if you try to call if after Lifecycle.Event.ON_STOP was dispatched

saveState

open fun saveState(): SavedState

Called to retrieve the state from a component before it is killed so the state can be retrieved later from SavedStateRegistry.consumeRestoredStateForKey.

Returns
SavedState

The SavedState containing the saved state.

unregisterSavedStateProvider

@MainThread
fun unregisterSavedStateProvider(key: String): Unit

Unregisters a component previously registered with the given key.

Parameters
key: String

The key with which the component was previously registered.

Public properties

isRestored

val isRestoredBoolean

Returns true if state has been restored and can be safely consumed with consumeRestoredStateForKey, false otherwise.