A simple and complete theory guide to understand and remember MVVM in Android.
MVVM (Model–View–ViewModel) is a software architecture pattern used to clearly separate:
- UI (View)
- UI Logic (ViewModel)
- Data Layer (Model/Repository)
This keeps your Android app clean, maintainable, testable, and scalable.
- ✔ Separation of concerns
- ✔ Cleaner Activities & Fragments
- ✔ Easier testing
- ✔ Reusable data logic
- ✔ ViewModel survives rotation
- ✔ Improved maintainability
- Handles UI rendering
- Observes LiveData / StateFlow from ViewModel
- Sends user actions to ViewModel
- No business logic here
- Holds UI logic
- Talks to the Repository
- Exposes LiveData/StateFlow for UI
- Survives configuration changes
- Does not know about Activity/Fragment
-
Contains all data operations
-
Fetches data from:
- Room Database
- REST API
- Cache
- SharedPreferences
-
ViewModel → Calls Repository → Gets data
- Creates ViewModel objects with required dependencies
- Needed when ViewModel requires parameters (e.g., Repository)
View (UI)
↓ user actions
ViewModel
↓ data request
Repository (Model)
↓ fetches DB/API
ViewModel
↓ updates LiveData
View observes LiveData → UI updates
- Write all data-related functions
- Link with Room or API
- Inject repository
- Prepare and expose data for UI
- Hold app logic
- Pass dependencies to ViewModel
- Required when using custom ViewModel constructors
- Initialize ViewModel with Factory
- Observe LiveData
- Update UI
- Send actions to ViewModel
MVVM = View shows → ViewModel thinks → Repository fetches.
If you want, I can also generate a PDF version of this README.