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

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 functions

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
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 functions

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