Member-only story
Keep Your ViewModels Clean: A Guide to Event-Driven Architecture in Android
Published in
5 min readSep 5, 2024

Hey guys I have come up with another Article on keeping your ViewModels clean and easy and the ViewModel Architecture I am showing is best for Compose.
Prerequisites
- Knowledge of basic Architecture
- Knowledge of basic Flows
Let's begin by understanding the terms you will see here for managing ViewModels.
The Core Concepts: Actions, Events, and States
Breaking down the user interactions into actions, events, and states is crucial for keeping your ViewModel clean and maintainable.
- Actions: These represent user inputs or UI interactions. Actions define what the user wants to do, such as changing a text field, clicking a button, or toggling a switch. By encapsulating all possible user interactions in a sealed class or enum, you centralize the logic for handling these actions.
- Events: Events are outputs from the ViewModel to the UI. They represent one-time effects, like showing a toast, navigating to another screen, or displaying an error message. Using a
Channel
orSharedFlow
for events ensures that the UI receives these events without needing to observe them continuously.