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
- Why Use a ViewModel?
- Exposing State from ViewModel
- The Android Lifecycle
- collectAsStateWithLifecycle