androidx.lifecycle.viewmodel


Interfaces

CreationExtras.Key

Key for the elements of CreationExtras.

Cmn
ViewModelStoreProvider.ReferenceToken

Represents an active hold on a specific ViewModelStore.

Cmn

Classes

CreationExtras

A map-like object holding pairs of CreationExtras.Key and Any, enabling efficient value retrieval for each key.

Cmn
InitializerViewModelFactoryBuilder

DSL for constructing a new ViewModelProvider.Factory

Cmn
MutableCreationExtras

A modifiable CreationExtras that holds pairs of CreationExtras.Key and Any, allowing efficient value retrieval for each key.

Cmn
ViewModelInitializer

Holds a ViewModel class and initializer for that class

Cmn
android
N
ViewModelStoreProvider

Manages a set of ViewModelStore instances scoped to a parent ViewModelStore.

Cmn

Objects

CreationExtras.Empty

An empty read-only CreationExtras.

Cmn

Annotations

Top-level functions summary

CreationExtras
CreationExtras(initialExtras: CreationExtras, builderAction: MutableCreationExtras.() -> Unit)

Builds a new read-only CreationExtras starting with the given initialExtras.

Cmn
ViewModelStoreOwner
ViewModelStoreOwner(
    viewModelStore: ViewModelStore,
    defaultArgs: SavedState,
    defaultCreationExtras: CreationExtras,
    defaultFactory: ViewModelProvider.Factory
)

Creates a ViewModelStoreOwner by composing a store, factory, and creation extras.

Cmn
ViewModelStoreOwner
ViewModelStoreOwner(
    viewModelStore: ViewModelStore,
    savedStateRegistryOwner: SavedStateRegistryOwner,
    defaultArgs: SavedState,
    defaultCreationExtras: CreationExtras,
    defaultFactory: ViewModelProvider.Factory
)

Creates a ViewModelStoreOwner that also acts as a SavedStateRegistryOwner, composing a store, state registry, lifecycle, factory, and creation extras.

Cmn
ViewModelStoreOwner
ViewModelStoreOwner(
    viewModelStore: ViewModelStore,
    savedStateRegistry: SavedStateRegistry,
    lifecycle: Lifecycle,
    defaultArgs: SavedState,
    defaultCreationExtras: CreationExtras,
    defaultFactory: ViewModelProvider.Factory
)

Creates a ViewModelStoreOwner that also acts as a SavedStateRegistryOwner, composing a store, state registry, lifecycle, factory, and creation extras.

Cmn
inline ViewModelProvider.Factory

Creates an InitializerViewModelFactory with the initializers provided in the builder.

Cmn

Extension functions summary

operator Boolean

Checks if the CreationExtras contains the given key.

Cmn
inline Unit
<VM : ViewModel> InitializerViewModelFactoryBuilder.initializer(
    noinline initializer: CreationExtras.() -> VM
)

Add an initializer to the InitializerViewModelFactoryBuilder

Cmn
operator MutableCreationExtras

Creates a new read-only CreationExtras by replacing or adding entries to this extras from another creationExtras.

Cmn
operator Unit

Appends or replaces all entries from the given creationExtras in this mutable extras.

Cmn

Top-level functions

CreationExtras

fun CreationExtras(
    initialExtras: CreationExtras = Empty,
    builderAction: MutableCreationExtras.() -> Unit = {}
): CreationExtras

Builds a new read-only CreationExtras starting with the given initialExtras.

You can customize the extras further by applying a builderAction on a MutableCreationExtras.

The MutableCreationExtras passed as a receiver to the builderAction is valid only inside that function. Using it outside of the function produces an unspecified behavior.

fun ViewModelStoreOwner(
    viewModelStore: ViewModelStore,
    defaultArgs: SavedState = savedState(),
    defaultCreationExtras: CreationExtras = CreationExtras.Empty,
    defaultFactory: ViewModelProvider.Factory = SavedStateViewModelFactory()
): ViewModelStoreOwner

Creates a ViewModelStoreOwner by composing a store, factory, and creation extras.

fun ViewModelStoreOwner(
    viewModelStore: ViewModelStore,
    savedStateRegistryOwner: SavedStateRegistryOwner,
    defaultArgs: SavedState = savedState(),
    defaultCreationExtras: CreationExtras = CreationExtras.Empty,
    defaultFactory: ViewModelProvider.Factory = SavedStateViewModelFactory()
): ViewModelStoreOwner

Creates a ViewModelStoreOwner that also acts as a SavedStateRegistryOwner, composing a store, state registry, lifecycle, factory, and creation extras.

While the simpler ViewModelStoreOwner factory handles standard ViewModels, this overload provides the necessary infrastructure for ViewModels that rely on SavedStateHandle. By bundling the SavedStateRegistryOwner with the store, it creates a fully equipped scope capable of properly wiring up state-saving mechanisms for its ViewModels.

fun ViewModelStoreOwner(
    viewModelStore: ViewModelStore,
    savedStateRegistry: SavedStateRegistry,
    lifecycle: Lifecycle,
    defaultArgs: SavedState = savedState(),
    defaultCreationExtras: CreationExtras = CreationExtras.Empty,
    defaultFactory: ViewModelProvider.Factory = SavedStateViewModelFactory()
): ViewModelStoreOwner

Creates a ViewModelStoreOwner that also acts as a SavedStateRegistryOwner, composing a store, state registry, lifecycle, factory, and creation extras.

While the simpler ViewModelStoreOwner factory handles standard ViewModels, this overload provides the necessary infrastructure for ViewModels that rely on SavedStateHandle. By bundling the SavedStateRegistry and Lifecycle with the store, it creates a fully equipped scope capable of properly wiring up state-saving mechanisms for its ViewModels.

viewModelFactory

inline fun viewModelFactory(builder: InitializerViewModelFactoryBuilder.() -> Unit): ViewModelProvider.Factory

Creates an InitializerViewModelFactory with the initializers provided in the builder.

Extension functions

contains

operator fun CreationExtras.contains(key: CreationExtras.Key<*>): Boolean

Checks if the CreationExtras contains the given key.

This method allows to use the key in creationExtras syntax for checking whether an key is contained in the CreationExtras.

inline fun <VM : ViewModel> InitializerViewModelFactoryBuilder.initializer(
    noinline initializer: CreationExtras.() -> VM
): Unit

Add an initializer to the InitializerViewModelFactoryBuilder

operator fun CreationExtras.plus(creationExtras: CreationExtras): MutableCreationExtras

Creates a new read-only CreationExtras by replacing or adding entries to this extras from another creationExtras.

The returned CreationExtras preserves the entry iteration order of the original CreationExtras.

Those entries of another creationExtras that are missing in this extras are iterated in the end in the order of that creationExtras.

plusAssign

operator fun MutableCreationExtras.plusAssign(creationExtras: CreationExtras): Unit

Appends or replaces all entries from the given creationExtras in this mutable extras.