SavedStateRegistry



An interface for plugging in components that consume and contribute to the saved state.

This object's lifetime is bound to the lifecycle of the owning component. When the activity or fragment is recreated, a new instance of this object is created as well.

Summary

Nested types

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

Contributes to the saved state.

Public functions

SavedState?

Consumes the saved state previously supplied by a SavedStateProvider registered via registerSavedStateProvider 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
Unit

Executes the given class when the owning component restarted.

android
Unit

Unregisters a component previously registered with the given key.

Cmn
android

Public properties

Boolean

Returns true if the state was restored after creation and can be safely consumed with consumeRestoredStateForKey, false otherwise.

Cmn
android

Public functions

consumeRestoredStateForKey

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

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

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

All unconsumed values are saved during state saving.

Call this method after the corresponding component has been created. Calling it before creation results in an IllegalArgumentException. Lifecycle.Event.ON_CREATE signals that a saved state can be safely consumed.

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 the state saving phase. The returned state will be associated with the given key and can be consumed after restoration via consumeRestoredStateForKey.

If there is an unconsumed value with the same key, the value supplied by the SavedStateProvider overrides it and is written to the resulting saved state.

If a provider was already registered with the given key, throws an IllegalArgumentException.

Parameters
key: String

The key with which the returned saved state is associated.

provider: SavedStateRegistry.SavedStateProvider

The SavedStateProvider to get the saved state.

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

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 the state was restored after creation and can be safely consumed with consumeRestoredStateForKey, false otherwise.