# 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)

*** ** * ** ***

Common/AllAndroid/JVM
- Cmn

  ```
  class ViewModelProvider
  ```
- android

  ```
  open class ViewModelProvider
  ```

*** ** * ** ***

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

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

1. **Caching / Retrieval** : It checks a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` to see if an instance of the requested `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/viewmodel/MutableCreationExtras` with the unique registration key under `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#VIEW_MODEL_KEY()`, combining it with default `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras` provided by the owner.

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

To ensure that `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances survive configuration changes, the underlying `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` must be retained (for example, by using a `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModel` instance:

```kotlin
        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/kotlin/androidx/lifecycle/viewmodel/CreationExtras` |   |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` |   |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` |   |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` |   |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` |   |

## Summary

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

| ### Public companion functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#create(androidx.lifecycle.ViewModelStoreOwner,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)( owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner, factory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory, extras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras )` Creates a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider` bound to the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner`. | Cmn android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#create(androidx.lifecycle.ViewModelStore,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)( store: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore, factory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory, extras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras )` Creates a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider` backed by the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore`. | Cmn android |

| ### Public companion properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras.Key<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#VIEW_MODEL_KEY()` A `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras.Key` used to retrieve the key associated with a requested `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`. | Cmn android |

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

| ### Public functions |
|---|---|---|
| `open operator T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#get(java.lang.Class)(modelClass: https://developer.android.com/reference/java/lang/Class.html<T>)` Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`. | android |
| `operator T` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#get(kotlin.reflect.KClass)(modelClass: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<T>)` Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`. | Cmn android |
| `open operator T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#get(kotlin.String,java.lang.Class)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, modelClass: https://developer.android.com/reference/java/lang/Class.html<T>)` Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`. | android |
| `operator T` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#get(kotlin.String,kotlin.reflect.KClass)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, modelClass: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<T>)` Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`. | Cmn android |

| ### Extension functions |
|---|---|---|
| `inline VM` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <VM : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#(androidx.lifecycle.ViewModelProvider).get()()` Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this `ViewModelProvider`. | Cmn |
| `inline VM` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <VM : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#(androidx.lifecycle.ViewModelProvider).get(kotlin.String)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` 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/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.ViewModelProvider).get(kotlin.String)`. | Cmn |

## Public companion functions

### create

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

```
fun create(
    owner: ViewModelStoreOwner,
    factory: ViewModelProvider.Factory = owner.defaultViewModelProviderFactory,
    extras: CreationExtras = owner.defaultViewModelCreationExtras
): ViewModelProvider
```

Creates a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider` bound to the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner`.

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

| Parameters |
|---|---|
| `owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` that manages the lifecycle of the created `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances |
| `factory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory = owner.defaultViewModelProviderFactory` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` responsible for creating new `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances |
| `extras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras = owner.defaultViewModelCreationExtras` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras` passed to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` to create the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` |

### create

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

```
fun create(
    store: ViewModelStore,
    factory: ViewModelProvider.Factory = DefaultViewModelProviderFactory,
    extras: CreationExtras = CreationExtras.Empty
): ViewModelProvider
```

Creates a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider` backed by the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore`.

The provider generates `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances using the specified `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` and stores them within the provided `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore`.

| Parameters |
|---|---|
| `store: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` where the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances are stored |
| `factory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory = DefaultViewModelProviderFactory` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` used to instantiate new `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances |
| `extras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras = CreationExtras.Empty` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras` passed to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` to create the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` |

## Public companion properties

### VIEW_MODEL_KEY

Cmn android

```
val VIEW_MODEL_KEY: CreationExtras.Key<String>
```

A `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras.Key` used to retrieve the key associated with a requested `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`.

The `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider` automatically includes the key in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras` passed to `https://developer.android.com/reference/kotlin/androidx/lifecycle/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 constructors

### ViewModelProvider

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

```
ViewModelProvider(owner: ViewModelStoreOwner)
```

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

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

### ViewModelProvider

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

```
ViewModelProvider(
    owner: ViewModelStoreOwner,
    factory: ViewModelProvider.Factory
)
```

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

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

### ViewModelProvider

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

```
ViewModelProvider(
    store: ViewModelStore,
    factory: ViewModelProvider.Factory,
    defaultCreationExtras: CreationExtras = CreationExtras.Empty
)
```

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

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

## Public functions

### get

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

```
open operator fun <T : ViewModel> get(modelClass: Class<T>): T
```

Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`.

The created `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `modelClass: https://developer.android.com/reference/java/lang/Class.html<T>` | `https://developer.android.com/reference/java/lang/Class.html` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` to retrieve or create |

| Returns |
|---|---|
| `T` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider#get(java.lang.Class)` is a local or anonymous class |

### get

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

```
@MainThread
operator fun <T : ViewModel> get(modelClass: KClass<T>): T
```

Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`.

The created `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` is associated with the given scope and is retained as long as the scope is alive (e.g., until the `Activity` is finished or the process is killed).

| Parameters |
|---|---|
| `modelClass: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<T>` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` to retrieve or create |

| Returns |
|---|---|
| `T` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#get(kotlin.reflect.KClass)` |

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

### get

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

```
open operator fun <T : ViewModel> get(key: String, modelClass: Class<T>): T
```

Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`.

The created `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | identifier of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` |
| `modelClass: https://developer.android.com/reference/java/lang/Class.html<T>` | `https://developer.android.com/reference/java/lang/Class.html` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` to retrieve or create |

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

### get

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

```
@MainThread
operator fun <T : ViewModel> get(key: String, modelClass: KClass<T>): T
```

Returns an existing `https://developer.android.com/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider`.

The created `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` is associated with the given scope and is retained as long as the scope is alive (e.g., until the `Activity` is finished or the process is killed).

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | identifier of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` |
| `modelClass: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<T>` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` to retrieve or create |

| Returns |
|---|---|
| `T` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider#get(kotlin.String,kotlin.reflect.KClass)` |

## Extension functions

### ViewModelProvider.get

Cmn 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
inline fun <VM : ViewModel> ViewModelProvider.get(): VM
```

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/kotlin/androidx/lifecycle/ViewModelProvider#get(kotlin.reflect.KClass)` | (Class) |

### ViewModelProvider.get

Cmn 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
inline fun <VM : ViewModel> ViewModelProvider.get(key: String): VM
```

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/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.ViewModelProvider).get(kotlin.String)`.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The key to use to identify the ViewModel. |

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