# ViewModel

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

*** ** * ** ***


```
public abstract class ViewModel
```

<br />

Known direct subclasses [AndroidViewModel](https://developer.android.com/reference/androidx/lifecycle/AndroidViewModel)

|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/AndroidViewModel` | `https://developer.android.com/reference/androidx/lifecycle/ViewModel` that has access to the `https://developer.android.com/reference/android/app/Application.html` context. |

*** ** * ** ***

Prepares and manages data for the UI.

ViewModels are scoped to a `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner` and are retained as long as their owner is alive. This retention allows ViewModels to persist across configuration changes (e.g., Android screen rotations), making the managed data immediately available to the new owner instance.

Examples of `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner`s include a `ComponentActivity` or a `Fragment`.

The diagram below visualizes the relationship between the owner, store, and ViewModels:

```
ViewModelStoreOwner
         |
         v (owns)
  ViewModelStore <================= Retained across configuration changes
   |          |
   v          v
ViewModelA  ViewModelB
              |
              v (destroys)
         onCleared() (called when owner is permanently destroyed)
```

Multiple UI components can share a single `https://developer.android.com/reference/androidx/lifecycle/ViewModel` to exchange data or coordinate state. To do this, resolve the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` using a shared, wider-scoped `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner` (such as the parent destination in a navigation graph, a parent fragment, or the containing activity).

A ViewModel's sole responsibility is managing UI state and data. It must never hold references to a `View` or any `Context` that could cause memory leaks.

An example in Compose:

```
class UserViewModel : ViewModel() {
    private val _user = MutableStateFlow<User?>(null)
    val user: StateFlow<User?> = _user.asStateFlow()
}

@Composable
fun UserScreen(viewModel: UserViewModel = viewModel()) {
    val user by viewModel.user.collectAsStateWithLifecycle()
    UserContent(user)
}
```

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

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/lifecycle/ViewModel#ViewModel()()` Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModel#ViewModel(kotlin.Array)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/AutoCloseable.html... closeables)` Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModel#ViewModel(kotlinx.coroutines.CoroutineScope)(@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html viewModelScope)` Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModel#ViewModel(kotlinx.coroutines.CoroutineScope,kotlin.Array)( @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html viewModelScope, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/AutoCloseable.html... closeables )` Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(java.lang.AutoCloseable)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/AutoCloseable.html closeable)` Adds an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/index.html` resource to this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |
| `final void` | `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(kotlin.String,java.lang.AutoCloseable)(@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/AutoCloseable.html closeable)` Adds an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/index.html` resource with an associated `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(kotlin.String,kotlin.AutoCloseable)` to this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |
| `final T` | `<T extends https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/index.html> https://developer.android.com/reference/androidx/lifecycle/ViewModel#getCloseable(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Returns the `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/index.html` resource associated to the given `https://developer.android.com/reference/androidx/lifecycle/ViewModel#getCloseable(kotlin.String)`, or `null` if such a `https://developer.android.com/reference/androidx/lifecycle/ViewModel#getCloseable(kotlin.String)` is not present in this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |

| ### Protected methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/EmptySuper https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()()` This method will be called when this `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is no longer used and will be destroyed. |

| ### Extension functions |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` | `https://developer.android.com/reference/androidx/lifecycle/ViewModelKt.https://developer.android.com/reference/androidx/lifecycle/ViewModel#(androidx.lifecycle.ViewModel).getViewModelScope()(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModel receiver)` The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` associated with this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |

## Public constructors

### ViewModel

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

```
public ViewModel()
```

Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

You should **never** manually create a `https://developer.android.com/reference/androidx/lifecycle/ViewModel` outside of a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory`.

### ViewModel

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

```
public ViewModel(@NonNull AutoCloseable... closeables)
```

Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

You should **never** manually create a `https://developer.android.com/reference/androidx/lifecycle/ViewModel` outside of a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/AutoCloseable.html... closeables` | resources to be closed when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is cleared, right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called |

### ViewModel

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

```
public ViewModel(@NonNull CoroutineScope viewModelScope)
```

Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

You should **never** manually create a `https://developer.android.com/reference/androidx/lifecycle/ViewModel` outside of a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html viewModelScope` | `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` to be canceled when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is cleared, right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called |

### ViewModel

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

```
public ViewModel(
    @NonNull CoroutineScope viewModelScope,
    @NonNull AutoCloseable... closeables
)
```

Creates a new `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

You should **never** manually create a `https://developer.android.com/reference/androidx/lifecycle/ViewModel` outside of a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html viewModelScope` | `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` to be canceled when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is cleared, right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/AutoCloseable.html... closeables` | resources to be closed when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is cleared, right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called |

## Public methods

### addCloseable

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

```
public void addCloseable(@NonNull AutoCloseable closeable)
```

Adds an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/index.html` resource to this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. The resource will be closed right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called.

If `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` has already been called, the provided resource will not be added and will be closed immediately.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/AutoCloseable.html closeable` | resource to be closed when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is cleared, right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called |

### addCloseable

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

```
public final void addCloseable(@NonNull String key, @NonNull AutoCloseable closeable)
```

Adds an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/index.html` resource with an associated `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(kotlin.String,kotlin.AutoCloseable)` to this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. The resource will be closed right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called.

If the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(kotlin.String,kotlin.AutoCloseable)` already has a resource associated with it, the old resource will be replaced and closed immediately.

If `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` has already been called, the provided resource will not be added and will be closed immediately.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | key to associate with the resource, for retrieval with `https://developer.android.com/reference/androidx/lifecycle/ViewModel#getCloseable(kotlin.String)` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/AutoCloseable.html closeable` | resource to be closed when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is cleared, right **before** the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` method is called |

### getCloseable

```
public final T <T extends AutoCloseable> getCloseable(@NonNull String key)
```

Returns the `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/index.html` resource associated to the given `https://developer.android.com/reference/androidx/lifecycle/ViewModel#getCloseable(kotlin.String)`, or `null` if such a `https://developer.android.com/reference/androidx/lifecycle/ViewModel#getCloseable(kotlin.String)` is not present in this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | key associated with a resource via `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(kotlin.String,kotlin.AutoCloseable)` |

## Protected methods

### onCleared

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

```
@EmptySuper
protected void onCleared()
```

This method will be called when this `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is no longer used and will be destroyed.

It is useful when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` observes data, and you need to clear the subscriptions to prevent a memory leak, as the subscriptions might hold a reference to the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` even after it is no longer needed.

**Clearing Sequence:**

1. `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/close.html` resources added **with** a key via `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(kotlin.String,kotlin.AutoCloseable)`.

2. `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/close.html` resources added via `constructor`.

3. `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-auto-closeable/close.html` resources added **without** a key via `https://developer.android.com/reference/androidx/lifecycle/ViewModel#addCloseable(kotlin.String,kotlin.AutoCloseable)`.

4. Invoke the `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` callback.

## Extension functions

### ViewModelKt.getViewModelScope

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

```
public final @NonNull CoroutineScope ViewModelKt.getViewModelScope(@NonNull ViewModel receiver)
```

The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` associated with this `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/coroutine-context.html` is configured with:

- `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/index.html`: ensures children jobs can fail independently of each other.

- `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-coroutine-dispatcher/immediate.html`: executes jobs immediately on the main (UI) thread. If the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-main.html` is not available on the current platform (e.g., Linux), we fall back to an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-empty-coroutine-context/index.html`.

This scope is automatically canceled when the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` is cleared, and can be replaced by using the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` constructor overload that takes in a `viewModelScope: CoroutineScope`.

For background execution, use `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/index.html` to switch to appropriate dispatchers (e.g., `kotlinx.coroutines.IO`).

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