CreationExtras

public abstract class CreationExtras


Simple map-like object that passed in ViewModelProvider.Factory.create to provide an additional information to a factory.

It allows making Factory implementations stateless, which makes an injection of factories easier because don't require all information be available at construction time.

Summary

Nested types

public static class CreationExtras.Empty extends CreationExtras

Empty CreationExtras

public interface CreationExtras.Key<T extends Object>

Key for the elements of CreationExtras.

Public methods

abstract T
<T extends Object> get(@NonNull CreationExtras.Key<@NonNull T> key)

Returns an element associated with the given key

Extension functions

final @NonNull SavedStateHandle

Creates SavedStateHandle that can be used in your ViewModels

Public methods

get

Added in 2.5.0
public abstract T <T extends Object> get(@NonNull CreationExtras.Key<@NonNull T> key)

Returns an element associated with the given key

Extension functions

SavedStateHandleSupport.createSavedStateHandle

@MainThread
public final @NonNull SavedStateHandle SavedStateHandleSupport.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