CreationExtras

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

Empty CreationExtras

interface CreationExtras.Key<T : Any?>

Key for the elements of CreationExtras.

Public functions

abstract operator T?
<T : Any?> get(key: CreationExtras.Key<T>)

Returns an element associated with the given key

Extension functions

SavedStateHandle

Creates SavedStateHandle that can be used in your ViewModels

Public functions

get

Added in 2.5.0
abstract operator fun <T : Any?> get(key: CreationExtras.Key<T>): T?

Returns an element associated with the given key

Extension functions

@MainThread
fun CreationExtras.createSavedStateHandle(): SavedStateHandle

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