# ViewModelStore

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

*** ** * ** ***

Common/AllAndroid/JVM
- Cmn

  ```
  open class ViewModelStore
  ```

*** ** * ** ***

Stores `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances by key.

A `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` instance **must** be retained across configuration changes. If an owner (typically a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner`) is destroyed and recreated due to a configuration change, the new owner must reuse the previous `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` instance.

When the owner is being destroyed permanently (i.e., it will not be recreated), it should call `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore#clear()` to notify all stored `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances that they are no longer needed (see `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel#onCleared()`).

Use `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner#viewModelStore()` to retrieve a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore` for activities and fragments.

### Not stable for inheritance

**This class is not intended for inheritance.** It is technically `open` for binary compatibility with previous versions, but extending this class is unsupported.

| See also |
|---|---|
| `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/ViewModelStoreOwner` |   |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore#ViewModelStore()()` | Cmn |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore#clear()()` Clears this store and notifies all stored `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances that they are no longer used. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStore#toString()()` | Cmn |

## Public constructors

### ViewModelStore

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

```
ViewModelStore()
```

## Public functions

### clear

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

```
fun clear(): Unit
```

Clears this store and notifies all stored `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instances that they are no longer used.

After this call, the store is empty.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel#onCleared()` |   |

### toString

Cmn

```
open fun toString(): String
```