SavedStateRegistry

public final class SavedStateRegistry


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

This objects lifetime is bound to the lifecycle of owning component: when activity or fragment is recreated, new instance of the object is created as well.

Summary

Nested types

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

This interface marks a component that contributes to saved state.

Public methods

final Bundle

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

final SavedStateRegistry.SavedStateProvider

Get a previously registered SavedStateProvider.

final boolean

Whether the state was restored after creation and can be safely consumed with consumeRestoredStateForKey.

final void

Registers a SavedStateProvider by the given key.

final void

Executes the given class when the owning component restarted.

final void

Unregisters a component previously registered by the given key

Public methods

consumeRestoredStateForKey

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

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

This call clears an internal reference to returned saved state, so if you call it second time in the row it will return null.

All unconsumed values will be saved during onSaveInstanceState(Bundle savedState)

This method can be called after super.onCreate(savedStateBundle) of the corresponding component. Calling it before that will result in IllegalArgumentException. Lifecycle.Event.ON_CREATE can be used as a signal that a saved state can be safely consumed.

Parameters
@NonNull String key

a key with which SavedStateProvider was previously registered.

Returns
Bundle

S with the previously saved state or {@code null}

getSavedStateProvider

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

Get a previously registered SavedStateProvider.

Parameters
@NonNull String key

The key used to register the SavedStateProvider when it was registered with registerSavedStateProvider(String, SavedStateProvider).

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

isRestored

Added in 1.0.0
@MainThread
public final boolean isRestored()

Whether the state was restored after creation and can be safely consumed with consumeRestoredStateForKey.

isRestored == true if state was restored

registerSavedStateProvider

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

Registers a SavedStateProvider by the given key. This savedStateProvider will be called during state saving phase, returned object will be associated with the given key and can be used after the restoration via .consumeRestoredStateForKey.

If there is unconsumed value with the same key, the value supplied by savedStateProvider will be overridden and will be written to resulting saved state.

If a provider was already registered with the given key, an implementation should throw an IllegalArgumentException

Parameters
@NonNull String key

a key with which returned saved state will be associated

@NonNull SavedStateRegistry.SavedStateProvider provider

savedStateProvider to get saved state.

runOnNextRecreation

Added in 1.0.0
@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
kotlin.IllegalArgumentException

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

unregisterSavedStateProvider

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

Unregisters a component previously registered by the given key

Parameters
@NonNull String key

a key with which a component was previously registered.