Architecture and UI State
Android Development with Compose: Architecture & Data
Course 5 · Chapter 1 · Organising an App
Android Development Fundamentals with Compose taught you to build screens. This course is about what sits behind them: where data lives, how it reaches the screen, and how the code stays understandable as the app grows. Before any new library, this chapter gives you the picture: layers, the way state and events travel, and the small class that describes what a screen is showing.
uiState as a StateFlow, modelling it as a data class or a sealed class, repositories between the UI and data sources,
and not sending one-off events from a ViewModel to the UI. General knowledge, not checked: the "no architecture" example, the folder layout, and the MVVM wording. All code is unrun (no Kotlin compiler on the machine used to write this). Nothing here was seen on your install. The names NotesUiState, NotesScreen
and the ComposeHelloTheme in the previews are examples; use your own project's.
The Problem Architecture Solves
Here is a screen written the easy way, with everything inside the composable:
(general knowledge) It works, until it grows. Typical troubles:
- It cannot be tested without drawing a screen, because the logic is welded to the UI.
- It cannot be previewed in Android Studio, because the preview would try to call the network.
- State is lost on rotation (Chapter 2 of Android Development Fundamentals with Compose), and every screen repeats the same loading code.
- Several screens need the same data, so the same fetching code is copied around.
Layers
(docs) Android's architecture guidance recommends at least two layers, plus an optional third:
| Layer | Job | Lives in |
|---|---|---|
| UI layer | Displays application data and takes user input. In Compose: your composables, plus the state holder (a ViewModel) that prepares what they show. | ui/ |
| Data layer | Holds the business logic and exposes app data. Repositories sit here and hide where data comes from (database, network, files). | data/ |
| Domain layer (optional) | Reusable business rules ("use cases"). The docs say it is for reusing logic across ViewModels or simplifying a complex one, and mainly worth it for big apps. | domain/ |
(docs) Two of the recommendations that matter most: repositories sit between the UI and the data sources, so UI components never touch a database or network client directly; and layers talk to each other with coroutines and flows (Chapter 3).
Unidirectional Data Flow
(docs) Unidirectional data flow (UDF) means state flows down from a state holder to the UI, and events flow up from the UI. A tap becomes an event passed upward; the state holder updates the state; the UI redraws from the new state. It is the state hoisting you met in Android Development Fundamentals with Compose 4, applied to the whole screen.
The benefit: there is one place where the state changes, so a bug in what is shown has one place to look.
UI State: One Class for What the Screen Shows
(docs) The recommendation is to expose a single uiState property from the state holder, modelled as a data class holding the data, errors and a loading flag, or a sealed class when the states are mutually exclusive. Both shapes:
Both are called NotesUiState here, so use one at a time; the screen below uses the sealed interface.
| Data class (flags and fields) | Sealed interface (one case at a time) | |
|---|---|---|
| Fits when | Several things can be true together (content showing while a refresh runs) | The screen is exactly one of loading, content or error |
| Impossible states | Possible (loading and error at once) | Not representable |
| Reading it in the UI | if on the fields | An exhaustive when |
A Stateless Screen from a UI State
With a state class, the screen becomes a plain function from state to UI, with events as callbacks. That also fixes the preview problem: pass in any state you like.
(docs) The same advice applies to events: the docs say not to send one-off events from the ViewModel to the UI, but to process the event in the ViewModel and show the result through a state update. Chapter 2 builds the ViewModel that supplies this state.
MVVM in One Paragraph
(general knowledge, and the structure the old course called MVVM) Model is the data layer; View is the composables; ViewModel is the state holder in between: it asks the data layer for data and turns it into UI state. The diagram above is MVVM with Compose, with the arrows labelled the way Compose likes them.
StateFlow replaces LiveData as the default way to publish state.
Try It Yourself
Take the "no architecture" NotesScreen above and list each responsibility it has (drawing, holding state, loading, parsing, and so on). Sort them into UI layer, data layer or "state holder", and say what the screen composable would be left with.
Pick a screen of your own (a weather forecast, a recipe list, a to-do list). Write a UI state for it twice: once as a data class and once as a sealed interface. Say which you prefer for that screen and why.
📄 View solutionBuild the stateless NotesScreen and write three @Preview functions, one each for loading, success and error. Check all three in the preview without running the app.
uiState exposed as a StateFlow; data class or sealed class for UI state; no one-off events from the ViewModel to the UI.General knowledge, not checked: the problems list for the no-architecture version, the folder layout, the MVVM summary, the data class against sealed interface table.
Not verified: every listing and solution (unrun); the imports; the exercises (not run by me). Nothing was checked on your install.
Sources: Guide to app architecture (developer.android.com), Recommendations for Android architecture (developer.android.com), UI layer (developer.android.com), UI State production (developer.android.com), Compose UI Architecture (developer.android.com).
ViewModel that exposes a StateFlow of UI state, how a composable collects it, and why the state survives rotation.