public abstract class ViewModel

Known direct subclasses
AndroidViewModel

ViewModel that has access to the Application context.


Prepares and manages data for the UI.

ViewModels are scoped to a 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 ViewModelStoreOwners 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 ViewModel to exchange data or coordinate state. To do this, resolve the ViewModel using a shared, wider-scoped 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)
}

Summary

Public constructors

Creates a new ViewModel.

Creates a new ViewModel.

ViewModel(@NonNull CoroutineScope viewModelScope)

Creates a new ViewModel.

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

Creates a new ViewModel.

Public methods

void

Adds an AutoCloseable resource to this ViewModel.

final void

Adds an AutoCloseable resource with an associated key to this ViewModel.

final T

Returns the AutoCloseable resource associated to the given key, or null if such a key is not present in this ViewModel.

Protected methods

void

This method will be called when this ViewModel is no longer used and will be destroyed.

Extension functions

final @NonNull CoroutineScope

The CoroutineScope associated with this ViewModel.

Public constructors

ViewModel

Added in 2.0.0
public ViewModel()

Creates a new ViewModel.

You should never manually create a ViewModel outside of a ViewModelProvider.Factory.

ViewModel

Added in 2.8.0
public ViewModel(@NonNull AutoCloseable... closeables)

Creates a new ViewModel.

You should never manually create a ViewModel outside of a ViewModelProvider.Factory.

Parameters
@NonNull AutoCloseable... closeables

resources to be closed when the ViewModel is cleared, right before the onCleared method is called

ViewModel

Added in 2.8.0
public ViewModel(@NonNull CoroutineScope viewModelScope)

Creates a new ViewModel.

You should never manually create a ViewModel outside of a ViewModelProvider.Factory.

Parameters
@NonNull CoroutineScope viewModelScope

CoroutineScope to be canceled when the ViewModel is cleared, right before the onCleared method is called

ViewModel

Added in 2.8.0
public ViewModel(
    @NonNull CoroutineScope viewModelScope,
    @NonNull AutoCloseable... closeables
)

Creates a new ViewModel.

You should never manually create a ViewModel outside of a ViewModelProvider.Factory.

Parameters
@NonNull CoroutineScope viewModelScope

CoroutineScope to be canceled when the ViewModel is cleared, right before the onCleared method is called

@NonNull AutoCloseable... closeables

resources to be closed when the ViewModel is cleared, right before the onCleared method is called

Public methods

addCloseable

Added in 2.8.0
public void addCloseable(@NonNull AutoCloseable closeable)

Adds an AutoCloseable resource to this ViewModel. The resource will be closed right before the onCleared method is called.

If onCleared has already been called, the provided resource will not be added and will be closed immediately.

Parameters
@NonNull AutoCloseable closeable

resource to be closed when the ViewModel is cleared, right before the onCleared method is called

addCloseable

Added in 2.8.0
public final void addCloseable(@NonNull String key, @NonNull AutoCloseable closeable)

Adds an AutoCloseable resource with an associated key to this ViewModel. The resource will be closed right before the onCleared method is called.

If the key already has a resource associated with it, the old resource will be replaced and closed immediately.

If onCleared has already been called, the provided resource will not be added and will be closed immediately.

Parameters
@NonNull String key

key to associate with the resource, for retrieval with getCloseable

@NonNull AutoCloseable closeable

resource to be closed when the ViewModel is cleared, right before the onCleared method is called

getCloseable

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

Returns the AutoCloseable resource associated to the given key, or null if such a key is not present in this ViewModel.

Parameters
@NonNull String key

key associated with a resource via addCloseable

Protected methods

onCleared

Added in 2.0.0
@EmptySuper
protected void onCleared()

This method will be called when this ViewModel is no longer used and will be destroyed.

It is useful when the ViewModel observes data, and you need to clear the subscriptions to prevent a memory leak, as the subscriptions might hold a reference to the ViewModel even after it is no longer needed.

Clearing Sequence:

  1. Close resources added with a key via addCloseable.

  2. Close resources added via constructor.

  3. Close resources added without a key via addCloseable.

  4. Invoke the onCleared callback.

Extension functions

ViewModelKt.getViewModelScope

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

The CoroutineScope associated with this ViewModel.

The CoroutineScope.coroutineContext is configured with:

This scope is automatically canceled when the ViewModel is cleared, and can be replaced by using the ViewModel constructor overload that takes in a viewModelScope: CoroutineScope.

For background execution, use kotlinx.coroutines.withContext to switch to appropriate dispatchers (e.g., kotlinx.coroutines.IO).

See also
onCleared