The Android Developer Challenge is back! Submit your idea before December 2.

ViewModelProvider

open class ViewModelProvider
kotlin.Any
   ↳ androidx.lifecycle.ViewModelProvider

An utility class that provides ViewModels for a scope.

Default ViewModelProvider for an Activity or a Fragment can be obtained by passing it to ViewModelProvider#ViewModelProvider(ViewModelStoreOwner).

Summary

Nested classes

open

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

abstract

Implementations of Factory interface are responsible to instantiate ViewModels.

open

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

Public constructors

<init>(@NonNull owner: ViewModelStoreOwner)

Creates ViewModelProvider.

<init>(@NonNull owner: ViewModelStoreOwner, @NonNull factory: ViewModelProvider.Factory)

Creates ViewModelProvider, which will create ViewModels via the given Factory and retain them in a store of the given ViewModelStoreOwner.

<init>(@NonNull store: ViewModelStore, @NonNull factory: ViewModelProvider.Factory)

Creates ViewModelProvider, which will create ViewModels via the given Factory and retain them in the given store.

Public methods

open T
get(@NonNull 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.

open T
get(@NonNull key: String, @NonNull 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.

Extension functions

From androidx.lifecycle
VM

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

Public constructors

<init>

ViewModelProvider(@NonNull owner: ViewModelStoreOwner)

Creates ViewModelProvider. This will create ViewModels and retain them in a store of the given ViewModelStoreOwner.

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

<init>

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

Creates ViewModelProvider, which will create ViewModels via the given Factory and retain them in a store of the given ViewModelStoreOwner.

Parameters
owner ViewModelStoreOwner: a ViewModelStoreOwner whose ViewModelStore will be used to retain ViewModels
factory ViewModelStoreOwner: a Factory which will be used to instantiate new ViewModels

<init>

ViewModelProvider(@NonNull store: ViewModelStore, @NonNull factory: ViewModelProvider.Factory)

Creates ViewModelProvider, which will create ViewModels via the given Factory and retain them in the given store.

Parameters
store ViewModelStore: ViewModelStore where ViewModels will be stored.
factory ViewModelStore: factory a Factory which will be used to instantiate new ViewModels

Public methods

get

@NonNull @MainThread open fun <T : ViewModel!> get(@NonNull 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.
<T> Class<T>: The type parameter for the ViewModel.
Return
T: A ViewModel that is an instance of the given type T.

get

@NonNull @MainThread open fun <T : ViewModel!> get(@NonNull key: String, @NonNull 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 String: The class of the ViewModel to create an instance of it if it is not present.
<T> String: The type parameter for the ViewModel.
Return
T: A ViewModel that is an instance of the given type T.