ViewModelProvider


A utility class that provides ViewModels for a scope.

Default ViewModelProvider for an Activity or a Fragment can be obtained by passing it to the constructor: ViewModelProvider(myFragment)

Summary

Nested types

Factory which may create AndroidViewModel and ViewModel, which have an empty constructor.

Implementations of Factory interface are responsible to instantiate ViewModels.

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.

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

Creates a ViewModelProvider.

Cmn
android
N

Public companion properties

CreationExtras.Key<String>

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

Cmn
android
N

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
@MainThread
<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
N
open operator T
@MainThread
<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
N

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

Public companion functions

create

fun create(
    owner: ViewModelStoreOwner,
    factory: ViewModelProvider.Factory = ViewModelProviders.getDefaultFactory(owner),
    extras: CreationExtras = ViewModelProviders.getDefaultCreationExtras(owner)
): ViewModelProvider

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

The ViewModelStoreOwner that will manage the lifecycle of the created ViewModel instances.

factory: ViewModelProvider.Factory = ViewModelProviders.getDefaultFactory(owner)

The Factory responsible for creating new ViewModel instances.

extras: CreationExtras = ViewModelProviders.getDefaultCreationExtras(owner)

Additional data to be passed to the Factory during ViewModel creation.

create

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

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 = DefaultViewModelProviderFactory

factory a Factory which will be used to instantiate new ViewModels

extras: CreationExtras = CreationExtras.Empty

Additional data to be passed to the Factory during ViewModel creation.

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

The ViewModelStoreOwner that will manage the lifecycle of the created ViewModel instances.

factory: ViewModelProvider.Factory

The 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

The 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

@MainThread
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 process is killed).

Parameters
modelClass: Class<T>

The class of the ViewModel to create an instance of it if it is not present.

Returns
T

A ViewModel that is an instance of the given type T.

Throws
kotlin.IllegalArgumentException

if the given modelClass is 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 will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).

Parameters
modelClass: KClass<T>

The class of the ViewModel to create an instance of it if it is not present.

Returns
T

A ViewModel that is an instance of the given type T.

Throws
kotlin.IllegalArgumentException

if the given modelClass is local or anonymous class.

get

@MainThread
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 process is killed).

Parameters
key: String

The key to use to identify the ViewModel.

modelClass: Class<T>

The class of the ViewModel to create an instance of it if it is not present.

Returns
T

A ViewModel that is an instance of the given 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 will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).

Parameters
key: String

The key to use to identify the ViewModel.

modelClass: KClass<T>

The class of the ViewModel to create an instance of it if it is not present.

Returns
T

A ViewModel that is an instance of the given type T.

Extension functions

@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)