- Put screen state and screen-related decisions in the ViewModel.
- Keep rendering, animations, and direct UI interactions in the UI.
- Send user actions from the UI to the ViewModel as clear inputs.
- Keep durable data and shared business rules in repositories or domain logic.
A useful boundary is to ask whether a piece of work describes what the screen should show or how the screen draws and interacts. The ViewModel prepares screen state and responds to user intent; the UI renders that state and handles framework-specific presentation.
This is a working guide, not a requirement to put every decision in a ViewModel. The right split keeps behavior testable without making UI code awkward or turning the ViewModel into a second UI toolkit.
Use one rule to choose the layer
Put logic in the ViewModel when it produces or changes screen state, coordinates a user action, or needs to continue across a UI configuration change. Keep it in the UI when it directly controls rendering, visual behavior, or an Android UI API.
| Task | Usually belongs in | Reason |
|---|---|---|
| Load items and expose loading state | ViewModel | It prepares screen state |
| Choose a color or animate a panel | UI | It is visual presentation |
| Validate and submit a form | ViewModel or domain layer | It coordinates an action |
| Launch a permission prompt | UI | It uses a UI-owned API |
| Save durable app data | Repository or data layer | It outlives the screen |
Let the ViewModel decide what the screen means; let the UI decide how that meaning looks.
If moving a piece of code out of the composable makes it easier to test without Android UI objects, it is often a good ViewModel or domain-layer candidate.
Put screen state and decisions in the ViewModel
A ViewModel is a screen-facing state holder, not a place for every line of application logic. It can combine data from repositories, track transient screen values, and expose a coherent state that the UI can observe.
- Expose meaningful state. Prefer a state object with fields such as items, query, loading status, and an error message over unrelated values that make invalid combinations easy.
- Handle user intent. Give actions clear entry points such as
onQueryChangedoronRetry, then update state or call the appropriate domain or data layer. - Keep business rules reusable. If a rule applies across screens or app entry points, put it in a domain component or another appropriate shared layer rather than hiding it in one screen's ViewModel.
- Keep dependencies appropriate. Use repositories or injected services for data access. Avoid holding an Activity, View, or other short-lived UI object in a ViewModel.
In Compose, the UI can collect a ViewModel's state with lifecycle-aware collection, such as collectAsStateWithLifecycle. The ViewModel should not depend on Compose types just to expose that state.
A ViewModel can survive configuration changes, but it is not durable storage. Use a repository or database for data that must remain available after the screen or process is gone.
Keep rendering and direct UI behavior in the UI
The UI turns state into pixels and turns taps, typing, and gestures into action calls. In Compose, that often means reading state in a composable, displaying the right content, and passing callbacks down to interactive elements.
- Read the current state. Collect or receive the screen state, then use it to decide which content to display.
- Render each state clearly. Show loading, content, empty, and error presentations where they make sense for the screen.
- Forward user actions. Call a ViewModel function or an injected callback when a user submits, selects, or retries.
- Own visual details here. Keep focus, scroll position, animation progress, and layout-specific choices with the UI unless they represent meaningful screen state needed elsewhere.
A useful check is whether the code needs a composable scope, a UI controller, or a launcher tied to the current UI. If so, the UI is usually the natural owner. Keep such APIs out of the ViewModel; pass the result of an interaction back as an action when the app needs to update screen state.
- Use UI state for presentation-only values, such as whether a local menu is expanded.
- Use a ViewModel value when a value affects the screen's meaningful content or action flow.
- Do not move a small piece of UI code just to make every composable appear “dumb.”
Handle effects without blurring the boundary
Some interactions are not ordinary state updates: a permission request, navigation, or a message shown briefly after saving. Decide which layer owns the decision and which layer has the capability to perform the effect.

- Start with the user action. The UI reports an intent, such as a save tap, to the ViewModel.
- Perform app work. The ViewModel validates the request, calls the relevant logic, and updates state with the result.
- Use UI capabilities in the UI. The composable or UI host launches a permission prompt, performs navigation, or displays a snackbar when appropriate.
- Choose a delivery approach deliberately. For important outcomes, model information in state so it is not lost if collection pauses; for brief effects, use a design that fits the app's lifecycle and event-delivery needs.
A one-off event emitted while the UI is not collecting can be missed, while replaying it later can repeat an effect. Consider whether the outcome should instead be represented as state that the UI can acknowledge or clear.
There is no single event pattern that fits every app. The practical goal is to make ownership and delivery behavior explicit, especially when navigation or a message must not be duplicated or silently dropped.
Frequently asked questions
Use these answers as defaults, then adjust for the behavior your screen actually needs.
Should all state go in the ViewModel?
No. Put meaningful screen state there when it coordinates content or actions. Keep purely visual, local state in the UI when the ViewModel does not need to know about it.
Can a ViewModel hold an Android Context?
Avoid holding an Activity or UI context because its lifecycle may end while the ViewModel remains. For app-level needs, prefer an appropriate injected abstraction or application-scoped dependency.
Should network calls happen in a composable?
Usually, no. A user action or screen request should trigger app logic through the ViewModel and a repository or domain component, rather than starting an unmanaged request during rendering.
Does every screen need a ViewModel?
No. A simple UI with no meaningful state or coordination may not need one. Add a ViewModel when it clarifies ownership, lifecycle handling, or testability rather than as a formality.
Key takeaways
Keep the split based on responsibility, not on a rule that one layer must contain more code.
- The ViewModel prepares screen state and coordinates screen-related actions.
- The UI renders state and owns visual behavior and UI-specific APIs.
- Repositories and domain logic handle durable data and reusable business rules.
- For effects, make lifecycle and delivery behavior an explicit design choice.
Shipping a build? Have it tested on real Android devices with AndroidTesters. Learn more
Prepared with AI assistance and checked against AndroidTesters' editorial guidelines. AndroidTesters is not affiliated with Google. Rules and numbers change: confirm important details in the official documentation before you act.