# ViewModelProvider

Artifact: [androidx.lifecycle:lifecycle-viewmodel](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/ViewModelProvider.+class:androidx.lifecycle.ViewModelProvider) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

*** ** * ** ***


```
public final class ViewModelProvider
```

<br />

*** ** * ** ***

A utility class that manages the lifecycle, caching, and instantiation of `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances.

A `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` acts as the central orchestrator that coordinates:

1. **Caching / Retrieval** : It checks a `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` to see if an instance of the requested `https://developer.android.com/reference/androidx/lifecycle/ViewModel` class already exists under a given key. If found, it returns the cached instance.

2. **Extras Injection** : It automatically populates a `https://developer.android.com/reference/androidx/lifecycle/viewmodel/MutableCreationExtras` with the unique registration key under `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Companion#VIEW_MODEL_KEY()`, combining it with default `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras` provided by the owner.

3. **Instantiation** : If no cached instance exists, it invokes a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` to create a new instance using the prepared extras, caches it in the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore`, and returns it.

To ensure that `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances survive configuration changes, the underlying `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` must be retained (for example, by using a `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner` such as `ComponentActivity` or `Fragment` which automatically handles this retention).

The following diagram illustrates the retrieval and creation flow of a `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instance:

```
        ViewModelProvider.get(key)
                    |
                    v
           Is ViewModel cached
           in ViewModelStore?
                 /   \
               Yes    No
               /       \
              v         v
    Return cached      Create via Factory & CreationExtras,
    ViewModel          cache in ViewModelStore, and return
```

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras` |   |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModel` |   |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` |   |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` |   |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner` |   |

## Summary

| ### Nested types |
|---|
| `public class https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.AndroidViewModelFactory extends https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory` Factory that creates `https://developer.android.com/reference/androidx/lifecycle/AndroidViewModel` and `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances. |
| `public static class https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Companion` |
| `public interface https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` Implementations of the `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` interface are responsible for instantiating `https://developer.android.com/reference/androidx/lifecycle/ViewModel`s. |
| `public class https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory implements https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` Factory that creates `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances by calling their no-argument constructor. |
| `public class https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory implements https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` Simple factory, which calls empty constructor on the give class. |
| `public static class https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.Companion` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner owner)` Creates `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.lifecycle.ViewModelProvider.Factory)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner owner, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory )` Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#ViewModelProvider(androidx.lifecycle.ViewModelStore,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelStore store, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras defaultCreationExtras )` Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#get(java.lang.Class)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass)` Returns an existing `https://developer.android.com/reference/androidx/lifecycle/ViewModel` or creates a new one in the scope (usually, a `Fragment` or an `Activity`) associated with this `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#get(kotlin.String,java.lang.Class)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass)` Returns an existing `https://developer.android.com/reference/androidx/lifecycle/ViewModel` or creates a new one in the scope (usually, a `Fragment` or an `Activity`) associated with this `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. |

| ### Extension functions |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull VM` | `@https://developer.android.com/reference/androidx/annotation/MainThread <VM extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/lifecycle/ViewModelProviderGetKt.https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#(androidx.lifecycle.ViewModelProvider).get()( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider receiver )` Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this `ViewModelProvider`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull VM` | `@https://developer.android.com/reference/androidx/annotation/MainThread <VM extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/lifecycle/ViewModelProviderGetKt.https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#(androidx.lifecycle.ViewModelProvider).get(kotlin.String)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key )` Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this `ViewModelProvider` and the given `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.ViewModelProvider).get(kotlin.String)`. |

## Public constructors

### ViewModelProvider

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0)

```
public ViewModelProvider(@NonNull ViewModelStoreOwner owner)
```

Creates `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. This will create `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances and retain them in the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` of the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner`.

This method will use the `https://developer.android.com/reference/androidx/lifecycle/HasDefaultViewModelProviderFactory#defaultViewModelProviderFactory()` if the owner implements `https://developer.android.com/reference/androidx/lifecycle/HasDefaultViewModelProviderFactory`. Otherwise, a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory` will be used.

### ViewModelProvider

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

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

Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. This provider generates `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances using the specified `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` and stores them within the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` of the provided `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner owner` | `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner` that will manage the lifecycle of the created `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory` | `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` responsible for creating new `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances |

### ViewModelProvider

Added in [2.5.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.5.0)

```
public ViewModelProvider(
    @NonNull ViewModelStore store,
    @NonNull ViewModelProvider.Factory factory,
    @NonNull CreationExtras defaultCreationExtras
)
```

Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`. This provider generates `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances using the specified `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` and stores them within the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` of the provided `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelStore store` | `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` where `https://developer.android.com/reference/androidx/lifecycle/ViewModel`s will be stored |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory` | `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` responsible for creating new `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras defaultCreationExtras` | additional data to be passed to the `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` during `https://developer.android.com/reference/androidx/lifecycle/ViewModel` creation |

## Public methods

### get

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

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

Returns an existing `https://developer.android.com/reference/androidx/lifecycle/ViewModel` or creates a new one in the scope (usually, a `Fragment` or an `Activity`) associated with this `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`.

The created `https://developer.android.com/reference/androidx/lifecycle/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 the process is killed).

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass` | `https://developer.android.com/reference/java/lang/Class.html` of the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` to retrieve or create |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#get(java.lang.Class)` |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#get(java.lang.Class)` is a local or anonymous class |

### get

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

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

Returns an existing `https://developer.android.com/reference/androidx/lifecycle/ViewModel` or creates a new one in the scope (usually, a `Fragment` or an `Activity`) associated with this `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`.

The created `https://developer.android.com/reference/androidx/lifecycle/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 the process is killed).

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | identifier of the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass` | `https://developer.android.com/reference/java/lang/Class.html` of the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` to retrieve or create |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#get(kotlin.String,java.lang.Class)` |

## Extension functions

### ViewModelProviderGetKt.get

Artifact: [androidx.lifecycle:lifecycle-viewmodel](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/ViewModelProviderGet.kt+function: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 |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#get(kotlin.reflect.KClass)` | (Class) |

### ViewModelProviderGetKt.get

Artifact: [androidx.lifecycle:lifecycle-viewmodel](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/ViewModelProviderGet.kt+function:get)

```
@MainThread
public final @NonNull VM <VM extends ViewModel> ViewModelProviderGetKt.get(
    @NonNull ViewModelProvider receiver,
    @NonNull String key
)
```

Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this `ViewModelProvider` and the given `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.ViewModelProvider).get(kotlin.String)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key to use to identify the ViewModel. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider#get(kotlin.reflect.KClass)` | (String, Class) |