Exposing State from ViewModel
Drive Compose UI from a ViewModel.
Exposing State from ViewModel is a free Android Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Exposing with StateFlow
Use a MutableStateFlow internally and expose it as a read-only StateFlow.
class CounterViewModel : ViewModel() {
private val _count = MutableStateFlow(0)
val count: StateFlow<Int> = _count.asStateFlow()
fun increment() {
_count.value++
}
}Collecting StateFlow in Compose
The Composable converts the flow into Compose state with collectAsState() (or the lifecycle-aware variant covered later).
@Composable
fun CounterScreen(vm: CounterViewModel) {
val count by vm.count.collectAsState()
Text("Count: " + count)
}Why StateFlow Needs an Initial Value
A StateFlow always has a current value. That is why you must give it an initial value when you create it.
Any new collector immediately receives the latest value, never an empty stream.
Alternative: Compose mutableStateOf
For simple screen-local state inside the ViewModel you can also use Compose mutableStateOf, which Compose observes automatically.
class CounterViewModel : ViewModel() {
var count by mutableStateOf(0)
private set
fun increment() { count++ }
}StateFlow vs Compose State
- StateFlow is part of Kotlin coroutines and works anywhere, not just Compose. It is great for shared, lifecycle-aware streams.
- mutableStateOf is Compose-specific and very concise.
StateFlow is generally preferred for ViewModels in larger apps.
Modeling Screen State as One Object
Instead of many separate flows, group related fields into a single UI state data class. This keeps updates atomic and predictable.
data class ProfileUiState(
val name: String = "",
val isLoading: Boolean = false,
val error: String? = null
)Exposing the UI State Object
Expose one StateFlow of that state class. The UI reads everything from a single source of truth.
private val _uiState = MutableStateFlow(ProfileUiState())
val uiState: StateFlow<ProfileUiState> =
_uiState.asStateFlow()Updating Immutable State
Update the state by producing a copy with changed fields, never by mutating the old object.
fun load() {
_uiState.update { it.copy(isLoading = true) }
// ... fetch, then:
_uiState.update {
it.copy(isLoading = false, name = "Ada")
}
}Events Flow Up, State Flows Down
This is the core of unidirectional data flow:
- State flows down from ViewModel to UI.
- Events flow up from UI to ViewModel via function calls.
Quick Check
Test your understanding of exposing state.
Recap
You learned:
- Keep a private
MutableStateFlow, expose a read-onlyStateFlow. - Group related fields in a UI state data class.
- Update with
copy()viaupdate {}. - State flows down, events flow up.
Frequently asked questions
Is the “Exposing State from ViewModel” lesson free?
Yes — the full text of “Exposing State from ViewModel” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Exposing State from ViewModel”?
Drive Compose UI from a ViewModel. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Exposing State from ViewModel” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Use a ViewModel?
- Exposing State from ViewModel
- The Android Lifecycle
- collectAsStateWithLifecycle