SavedStateHandleSupport

Added in 2.5.0

public final class SavedStateHandleSupport


Summary

Public fields

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

A key for default arguments that should be passed to SavedStateHandle if needed.

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

A key for SavedStateRegistryOwner that corresponds to ViewModelStoreOwner of a ViewModel that is being created.

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

A key for ViewModelStoreOwner that is an owner of a ViewModel that is being created.

Public methods

static final @NonNull SavedStateHandle

Creates SavedStateHandle that can be used in your ViewModels

static final void

Enables the support of SavedStateHandle in a component.

Public fields

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

A key for default arguments that should be passed to SavedStateHandle if needed.

SAVED_STATE_REGISTRY_OWNER_KEY

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

A key for SavedStateRegistryOwner that corresponds to ViewModelStoreOwner of a ViewModel that is being created.

VIEW_MODEL_STORE_OWNER_KEY

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

A key for ViewModelStoreOwner that is an owner of a ViewModel that is being created.

Public methods

createSavedStateHandle

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

Creates SavedStateHandle that can be used in your ViewModels

This function requires enableSavedStateHandles call during the component initialization. Latest versions of androidx components like ComponentActivity, Fragment, NavBackStackEntry makes this call automatically.

This CreationExtras must contain SAVED_STATE_REGISTRY_OWNER_KEY, VIEW_MODEL_STORE_OWNER_KEY and VIEW_MODEL_KEY.

Throws
kotlin.IllegalArgumentException

if this CreationExtras are missing required keys: ViewModelStoreOwnerKey, SavedStateRegistryOwnerKey, VIEW_MODEL_KEY

enableSavedStateHandles

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

Enables the support of SavedStateHandle in a component.

After this method, createSavedStateHandle can be called on CreationExtras containing this SavedStateRegistryOwner / ViewModelStoreOwner.

Must be called while component is in INITIALIZED or CREATED state and before a ViewModel with SavedStateHandle is requested.