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

Public methods

@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.

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.

@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.

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.

Public constructors

Public methods

get

public @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.

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

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

get

@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.

get

public @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.

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 Class<@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.

get

@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

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.