ViewModelProvider


public final class 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

public interface ViewModelProvider.Factory

Implementations of Factory interface are responsible to instantiate ViewModels.

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

Public fields

static final @NonNull CreationExtras.Key<@NonNull String>

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

Public methods

static final @NonNull ViewModelProvider
create(
    @NonNull ViewModelStoreOwner owner,
    @NonNull ViewModelProvider.Factory factory,
    @NonNull CreationExtras extras
)

Creates a ViewModelProvider.

static final @NonNull ViewModelProvider
create(
    @NonNull ViewModelStore store,
    @NonNull ViewModelProvider.Factory factory,
    @NonNull CreationExtras extras
)

Creates a ViewModelProvider.

final @NonNull T
@MainThread
<T extends ViewModel> get(@NonNull KClass<@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.

final @NonNull T
@MainThread
<T extends ViewModel> get(@NonNull String key, @NonNull KClass<@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 @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 fields

VIEW_MODEL_KEY

public static final @NonNull CreationExtras.Key<@NonNull StringVIEW_MODEL_KEY

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 methods

create

Added in 2.8.0
public static final @NonNull ViewModelProvider create(
    @NonNull ViewModelStoreOwner owner,
    @NonNull ViewModelProvider.Factory factory,
    @NonNull CreationExtras extras
)

Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.

Parameters
@NonNull ViewModelStoreOwner owner

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

@NonNull ViewModelProvider.Factory factory

The Factory responsible for creating new ViewModel instances.

@NonNull CreationExtras extras

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

create

Added in 2.8.0
public static final @NonNull ViewModelProvider create(
    @NonNull ViewModelStore store,
    @NonNull ViewModelProvider.Factory factory,
    @NonNull CreationExtras extras
)

Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.

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

@NonNull CreationExtras extras

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

get

Added in 2.8.0
@MainThread
public final @NonNull T <T extends ViewModel> get(@NonNull KClass<@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 KClass<@NonNull T> modelClass

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

Returns
@NonNull 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

Added in 2.8.0
@MainThread
public final @NonNull T <T extends ViewModel> get(@NonNull String key, @NonNull KClass<@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 String key

The key to use to identify the ViewModel.

@NonNull KClass<@NonNull T> modelClass

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

Returns
@NonNull T

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

Extension functions

ViewModelProviderGetKt.get

@MainThread
public final @NonNull VM <VM extends ViewModel> ViewModelProviderGetKt.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.

See also
get

(Class)