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.

Public methods

@MainThread @NonNull T
<T extends ViewModel> get(@NonNull Class<@NonNull 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(@NonNull String key, @NonNull Class<@NonNull 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
<VM extends ViewModel> ViewModelProviderKt.get(
    @NonNull ViewModelProvider receiver
)

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 ViewModelProvider(@NonNull ViewModelStoreOwner owner)

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.

ViewModelProvider

public final ViewModelProvider(
    @NonNull ViewModelStoreOwner owner,
    @NonNull ViewModelProvider.Factory factory
)

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

Parameters
@NonNull ViewModelStoreOwner owner

a ViewModelStoreOwner whose ViewModelStore will be used to retain ViewModels

@NonNull ViewModelProvider.Factory factory

a Factory which will be used to instantiate new ViewModels

ViewModelProvider

public final ViewModelProvider(
    @NonNull ViewModelStore store,
    @NonNull ViewModelProvider.Factory factory
)
Parameters
@NonNull ViewModelStore store

ViewModelStore where ViewModels will be stored.

@NonNull ViewModelProvider.Factory factory

factory a Factory which will be used to instantiate new ViewModels

Public methods

get

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

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
@NonNull Class<@NonNull T> modelClass

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

Returns
@MainThread T

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

Throws
java.lang.IllegalArgumentException

if the given modelClass is local or anonymous class.

get

@MainTh