SavedStateRegistry


public final class SavedStateRegistry implements SavedStateProvider, SavedStateRestorer


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.

Creates a SavedStateRegistry initialized with initialState.

Public methods

final SavedState

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

final SavedStateRegistry.SavedStateProvider

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

final boolean

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

final void

Registers a SavedStateProvider with the given key.

void
restoreState(Bundle savedState)

Called to restore the state of a component.

final void

This method is deprecated. `runOnNextRecreation` is deprecated.

@NonNull SavedState

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

final void

Unregisters a component previously registered with the given key.

Public constructors

SavedStateRegistry

Added in 1.6.0-alpha02
public SavedStateRegistry()

Creates an empty SavedStateRegistry.

SavedStateRegistry

Added in 1.6.0-alpha02
public SavedStateRegistry(@NonNull Bundle initialState)

Creates a SavedStateRegistry initialized with initialState.

Parameters
@NonNull Bundle initialState

The initial saved state to restore from.

Public methods

consumeRestoredStateForKey

Added in 1.0.0
@MainThread
public final SavedState consumeRestoredStateForKey(@NonNull String key)

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
@NonNull String key

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

Added in 1.2.0
public final SavedStateRegistry.SavedStateProvider getSavedStateProvider(@NonNull String key)

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

Parameters
@NonNull String key

The key used to register the SavedStateProvider.

isRestored

Added in 1.0.0
@MainThread
public final boolean isRestored()

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

registerSavedStateProvider

Added in 1.0.0
@MainThread
public final void registerSavedStateProvider(
    @NonNull String key,
    @NonNull SavedStateRegistry.SavedStateProvider provider
)

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
@NonNull String key

The key to associate with the provider.

@NonNull SavedStateRegistry.SavedStateProvider provider

The SavedStateProvider to register.

restoreState

public void restoreState(Bundle savedState)

Called to restore the state of a component.

Parameters
Bundle savedState

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

runOnNextRecreation

Added in 1.0.0
Deprecated in 1.6.0-alpha02
@MainThread
public final void runOnNextRecreation(
    @NonNull Class<@NonNull SavedStateRegistry.AutoRecreated> clazz
)

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
@NonNull Class<@NonNull SavedStateRegistry.AutoRecreated> clazz

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

public @NonNull SavedState saveState()

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

Returns
@NonNull SavedState

The SavedState containing the saved state.

unregisterSavedStateProvider

Added in 1.0.0
@MainThread
public final void unregisterSavedStateProvider(@NonNull String key)

Unregisters a component previously registered with the given key.

Parameters
@NonNull String key

The key with which the component was previously registered.