SavedStateHandleSupport

Added in 2.5.0

public final class SavedStateHandleSupport


Summary

Public fields

static final @NonNull CreationExtras.Key<@NonNull SavedState>

A CreationExtras.Key to retrieve the default SavedState arguments to be passed to the SavedStateHandle.

static final @NonNull CreationExtras.Key<@NonNull SavedStateRegistryOwner>

A CreationExtras.Key to retrieve the SavedStateRegistryOwner associated with the ViewModel being created.

static final @NonNull CreationExtras.Key<@NonNull ViewModelStoreOwner>

A CreationExtras.Key to retrieve the ViewModelStoreOwner associated with the ViewModel being created.

Public methods

static final @NonNull SavedStateHandle

Creates a SavedStateHandle for use in your ViewModels.

static final void

Enables support for SavedStateHandle in a component.

Public fields

public static final @NonNull CreationExtras.Key<@NonNull SavedStateDEFAULT_ARGS_KEY

A CreationExtras.Key to retrieve the default SavedState arguments to be passed to the SavedStateHandle.

SAVED_STATE_REGISTRY_OWNER_KEY

public static final @NonNull CreationExtras.Key<@NonNull SavedStateRegistryOwnerSAVED_STATE_REGISTRY_OWNER_KEY

A CreationExtras.Key to retrieve the SavedStateRegistryOwner associated with the ViewModel being created.

VIEW_MODEL_STORE_OWNER_KEY

public static final @NonNull CreationExtras.Key<@NonNull ViewModelStoreOwnerVIEW_MODEL_STORE_OWNER_KEY

A CreationExtras.Key to retrieve the ViewModelStoreOwner associated with the ViewModel being created.

Public methods

createSavedStateHandle

@MainThread
public static final @NonNull SavedStateHandle createSavedStateHandle(@NonNull CreationExtras receiver)

Creates a SavedStateHandle for use in your ViewModels.

This function requires a call to enableSavedStateHandles during component initialization. Modern Jetpack components (such as ComponentActivity, Fragment, and NavBackStackEntry) perform this call automatically.

The CreationExtras must contain SAVED_STATE_REGISTRY_OWNER_KEY, VIEW_MODEL_STORE_OWNER_KEY, and VIEW_MODEL_KEY.

Returns
@NonNull SavedStateHandle

new SavedStateHandle instance

enableSavedStateHandles

@MainThread
public static final void <T extends SavedStateRegistryOwner & ViewModelStoreOwner> enableSavedStateHandles(
    @NonNull T receiver
)

Enables support for SavedStateHandle in a component.

After calling this method, CreationExtras.createSavedStateHandle can be invoked on CreationExtras containing the SavedStateRegistryOwner and ViewModelStoreOwner.

This must be called while the component is in the Lifecycle.State.INITIALIZED or Lifecycle.State.CREATED state, and before requesting any ViewModel that requires a SavedStateHandle.