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 |
|---|
public interface SavedStateRegistry.AutoRecreatedSubclasses of this interface will be automatically recreated if they were previously registered via |
public fun interface SavedStateRegistry.SavedStateProviderThis interface marks a component that contributes to saved state. |
Public methods |
|
|---|---|
final SavedState |
Consumes saved state previously supplied by |
final SavedStateRegistry.SavedStateProvider |
Get a previously registered |
final boolean |
Whether the state was restored after creation and can be safely consumed with |
final void |
@MainThreadRegisters a |
final void |
Executes the given class when the owning component restarted. |
final void |
Unregisters a component previously registered by the given |
Public methods
consumeRestoredStateForKey
@MainThread
public final SavedState 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(SavedState 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 |
| Returns | |
|---|---|
SavedState |
|
getSavedStateProvider
public final SavedStateRegistry.SavedStateProvider getSavedStateProvider(@NonNull String key)
Get a previously registered SavedStateProvider.
| Parameters | |
|---|---|
@NonNull String key |
The key used to register the Returns the |
isRestored
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
@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
@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 |
unregisterSavedStateProvider
@MainThread
public final void unregisterSavedStateProvider(@NonNull String key)
Unregisters a component previously registered by the given key