ViewModelProvider



A utility class that manages the lifecycle, caching, and instantiation of ViewModel instances.

A ViewModelProvider acts as the central orchestrator that coordinates:

  1. Caching / Retrieval: It checks a ViewModelStore to see if an instance of the requested ViewModel class already exists under a given key. If found, it returns the cached instance.

  2. Extras Injection: It automatically populates a MutableCreationExtras with the unique registration key under VIEW_MODEL_KEY, combining it with default CreationExtras provided by the owner.

  3. Instantiation: If no cached instance exists, it invokes a ViewModelProvider.Factory to create a new instance using the prepared extras, caches it in the ViewModelStore, and returns it.

To ensure that ViewModel instances survive configuration changes, the underlying ViewModelStore must be retained (for example, by using a ViewModelStoreOwner such as ComponentActivity or Fragment which automatically handles this retention).

The following diagram illustrates the retrieval and creation flow of a ViewModel instance:

        ViewModelProvider.get(key)
|
v
Is ViewModel cached
in ViewModelStore?
/ \
Yes No
/ \
v v
Return cached Create via Factory & CreationExtras,
ViewModel cache in ViewModelStore, and return

Summary

Nested types

Factory that creates AndroidViewModel and ViewModel instances.

Implementations of the Factory interface are responsible for instantiating ViewModels.

Factory that creates ViewModel instances by calling their no-argument constructor.

Simple factory, which calls empty constructor on the give class.

Public companion functions

ViewModelProvider
create(
    owner: ViewModelStoreOwner,
    factory: ViewModelProvider.Factory,
    extras: CreationExtras
)

Creates a ViewModelProvider bound to the given ViewModelStoreOwner.

Cmn
android
ViewModelProvider
create(
    store: ViewModelStore,
    factory: ViewModelProvider.Factory,
    extras: CreationExtras
)

Creates a ViewModelProvider backed by the given ViewModelStore.

Cmn
android

Public companion properties

CreationExtras.Key<String>

A CreationExtras.Key used to retrieve the key associated with a requested ViewModel.

Cmn
android

Public constructors

Creates ViewModelProvider.

android

Creates a ViewModelProvider.

android
ViewModelProvider(
    store: ViewModelStore,
    factory: ViewModelProvider.Factory,
    defaultCreationExtras: CreationExtras
)

Creates a ViewModelProvider.

android

Public functions

open operator T
<T : ViewModel> get(modelClass: Class<T>)

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

android
operator T
@MainThread
<T : ViewModel> get(modelClass: KClass<T>)

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

Cmn
android
open operator T
<T : ViewModel> get(key: String, modelClass: Class<T>)

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

android
operator T
@MainThread
<T : ViewModel> get(key: String, modelClass: KClass<T>)

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

Cmn
android

Extension functions

inline VM

Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider.

Cmn
inline VM

Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider and the given key.

Cmn

Public companion functions

create

fun create(
    owner: ViewModelStoreOwner,
    factory: ViewModelProvider.Factory = owner.defaultViewModelProviderFactory,
    extras: CreationExtras = owner.defaultViewModelCreationExtras
): ViewModelProvider

Creates a ViewModelProvider bound to the given ViewModelStoreOwner.

The provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the ViewModelStoreOwner.

Parameters
owner: ViewModelStoreOwner

ViewModelStoreOwner that manages the lifecycle of the created ViewModel instances

factory: ViewModelProvider.Factory = owner.defaultViewModelProviderFactory

Factory responsible for creating new ViewModel instances

extras: CreationExtras = owner.defaultViewModelCreationExtras

CreationExtras passed to the Factory to create the ViewModel

create

fun create(
    store: ViewModelStore,
    factory: ViewModelProvider.Factory = DefaultViewModelProviderFactory,
    extras: CreationExtras = CreationExtras.Empty
): ViewModelProvider

Creates a ViewModelProvider backed by the given ViewModelStore.

The provider generates ViewModel instances using the specified Factory and stores them within the provided ViewModelStore.

Parameters
store: ViewModelStore

ViewModelStore where the ViewModel instances are stored

factory: ViewModelProvider.Factory = DefaultViewModelProviderFactory

Factory used to instantiate new ViewModel instances

extras: CreationExtras = CreationExtras.Empty

CreationExtras passed to the Factory to create the ViewModel

Public companion properties

VIEW_MODEL_KEY

val VIEW_MODEL_KEYCreationExtras.Key<String>

A CreationExtras.Key used to retrieve the key associated with a requested ViewModel.

The ViewModelProvider automatically includes the key in the CreationExtras passed to ViewModelProvider.Factory. This applies to keys generated by either of these usage patterns:

  • ViewModelProvider.get(key, MyViewModel::class): provided key is used.

  • ViewModelProvider.get(MyViewModel::class): generates a key from given class.

Public constructors

ViewModelProvider

ViewModelProvider(owner: ViewModelStoreOwner)

Creates ViewModelProvider. This will create ViewModel instances and retain them in the ViewModelStore of the given ViewModelStoreOwner.

This method will use the default factory if the owner implements HasDefaultViewModelProviderFactory. Otherwise, a NewInstanceFactory will be used.

ViewModelProvider

ViewModelProvider(
    owner: ViewModelStoreOwner,
    factory: ViewModelProvider.Factory
)

Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.

Parameters
owner: ViewModelStoreOwner

ViewModelStoreOwner that will manage the lifecycle of the created ViewModel instances

factory: ViewModelProvider.Factory

Factory responsible for creating new ViewModel instances

ViewModelProvider

ViewModelProvider(
    store: ViewModelStore,
    factory: ViewModelProvider.Factory,
    defaultCreationExtras: CreationExtras = CreationExtras.Empty
)

Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.

Parameters
store: ViewModelStore

ViewModelStore where ViewModels will be stored

factory: ViewModelProvider.Factory

Factory responsible for creating new ViewModel instances

defaultCreationExtras: CreationExtras = CreationExtras.Empty

additional data to be passed to the Factory during ViewModel creation

Public functions

get

open operator fun <T : ViewModel> get(modelClass: Class<T>): T

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g., if it is an Activity, until it is finished or the process is killed).

Parameters
modelClass: Class<T>

Class of the ViewModel to retrieve or create

Returns
T

ViewModel instance of type T

Throws
IllegalArgumentException

if the given modelClass is a local or anonymous class

get

@MainThread
operator fun <T : ViewModel> get(modelClass: KClass<T>): T

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

The created ViewModel is associated with the given scope and is retained as long as the scope is alive (e.g., until the Activity is finished or the process is killed).

Parameters
modelClass: KClass<T>

KClass of the ViewModel to retrieve or create

Returns
T

ViewModel instance of type T

Throws
IllegalArgumentException

if the given modelClass is a local or anonymous class

get

open operator fun <T : ViewModel> get(key: String, modelClass: Class<T>): T

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g., if it is an Activity, until it is finished or the process is killed).

Parameters
key: String

identifier of the ViewModel

modelClass: Class<T>

Class of the ViewModel to retrieve or create

Returns
T

ViewModel instance of type T

get

@MainThread
operator fun <T : ViewModel> get(key: String, modelClass: KClass<T>): T

Returns an existing ViewModel or creates a new one in the scope (usually, a Fragment or an Activity) associated with this ViewModelProvider.

The created ViewModel is associated with the given scope and is retained as long as the scope is alive (e.g., until the Activity is finished or the process is killed).

Parameters
key: String

identifier of the ViewModel

modelClass: KClass<T>

KClass of the ViewModel to retrieve or create

Returns
T

ViewModel instance of type T

Extension functions

ViewModelProvider.get

@MainThread
inline fun <VM : ViewModel> ViewModelProvider.get(): VM

Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider.

See also
get

(Class)

ViewModelProvider.get

@MainThread
inline fun <VM : ViewModel> ViewModelProvider.get(key: String): VM

Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider and the given key.

Parameters
key: String

The key to use to identify the ViewModel.

See also
get

(String, Class)