ViewModelProvider

public class 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 the constructor: ViewModelProvider(myFragment)

Summary

Nested types

ViewModelProvider.AndroidViewModelFactory

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

ViewModelProvider.AndroidViewModelFactory.Companion
ViewModelProvider.Factory

Implementations of Factory interface are responsible to instantiate ViewModels.

ViewModelProvider.NewInstanceFactory

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

ViewModelProvider.NewInstanceFactory.Companion

Public constructors

Creates ViewModelProvider.

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

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

Public methods

@MainThread @NonNull T
<T extends ViewModel> get(Class<T> modelClass)

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

@MainThread @NonNull T
<T extends ViewModel> get(String key, Class<T> modelClass)

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

Extension functions

final @MainThread @NonNull 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

ViewModelProvider

public final