跪拜 Guibai
← All articles
Android · Android Jetpack · Kotlin

MVI Gives an Android Screen Exactly One State at a Time

By 稀有猿诉 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A screen whose state is a pile of booleans will eventually hit a race where the UI shows a loading spinner, an error, and partial content at once. MVI eliminates that entire class of defect by making the state a sealed type and a single source of truth, and the pattern is practical enough to adopt with Kotlin Flows and Compose today.

Summary

The bug where a loading spinner spins over an error message and a half-filled list has a specific root cause: the screen is described by five independent variables, giving 32 configurations when the team only designed four. MVI collapses those variables into one immutable snapshot. The screen's behavior is then defined by a pure reducer function that takes the current state and an event and returns the next state, with intents coming from user actions and effects modeled as one-time events.

Implementation details matter: use StateFlow with atomic update, a buffered Channel for effects, and Compose's exhaustive when on the sealed content type. The payoff is that the compiler catches missing branches, tests become mechanical, and out-of-order async results have a single place to be handled. But MVI is not free; it adds boilerplate, and it's only worthwhile on screens with multiple async sources and many visual states. Static screens and simple forms don't need it.

Takeaways
— MVI merges separate StateFlows into one immutable state object with a sealed Content type, so a loading flag and an error field can't both be true at the same time.
— The reducer is a pure function (current state + event -> next state); keeping it pure makes the ViewModel thin and tests trivial without coroutine scaffolding.
— Effects are one-time events delivered through a buffer Channel and collected with flowWithLifecycle, preventing Snackbars or navigation from replaying after configuration changes.
— Cancel the previous search job before launching a new one to stop a slow old response from overwriting a newer, faster result.
— Exhaustive when over the sealed Content interface forces the compiler to flag any screen that forgets to handle a newly added content branch.
— MVI carries real costs: state classes can become dumping grounds for nullable fields, passing a whole state object deep into Compose raises recomposition, and critical info like a completed payment belongs in state or persistence, not in a transient effect.
Conclusions

The pattern shifts correctness from runtime discipline to compile-time guarantees: a sealed interface with an exhaustive when makes an impossible state unrepresentable, which is a guarantee a pile of booleans can never offer.

The separation of effects from state is the aspect that catches teams off guard; one-time events should never survive a configuration change, and modeling them as a separate Channel forces that decision to be explicit.

MVI is a set of constraints accepted voluntarily, so the payoff scales with the number of async sources and visual states a screen coordinates; applying it to a static page adds ceremony without benefit.

Concepts & terms
MVI (Model-View-Intent)
An Android architecture pattern where the UI state is one immutable object, user actions are expressed as intents, and all state changes go through a pure reducer function. It guarantees the screen never exists in a combination of states that wasn't designed.
Reducer
A pure function (currentState, event) -> nextState. It contains no side effects and is the single place where UI state transitions are defined, making behavior easy to test and reason about.
Effect (in MVI)
A one-time event such as navigation, a Snackbar, or haptic feedback. It's modeled separately from state, often via a Channel, so that it isn't replayed when the screen is recreated on rotation or process restart.
Sealed interface
A Kotlin type that restricts a value to a fixed set of subclasses. Used for Content types or intents in MVI, it allows exhaustive when expressions, forcing the compiler to handle every possible branch.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗