0Pricing
Android Academy · Lesson

Exposing State from ViewModel

Drive Compose UI from a ViewModel.

Exposing State to the UI

A ViewModel holds state, but the UI needs a way to observe that state and re-render when it changes.

In modern Android two common tools do this: StateFlow and Compose State.

The Mutable vs Immutable Pattern

A best practice is to keep a private mutable backing property and expose a public read-only version.

This prevents the UI from accidentally mutating state directly.

All lessons in this course

  1. Why Use a ViewModel?
  2. Exposing State from ViewModel
  3. The Android Lifecycle
  4. collectAsStateWithLifecycle
← Back to Android Academy