📘 Introduction
Welcome to Day 11 of the HandsOnAndroid 90-Day Android Journey! After introducing MVVM Architecture on Day 10, it’s time to explore the ViewModel—a core component of modern Android development.
The ViewModel allows your app to handle UI-related data in a lifecycle-conscious manner, making your apps more robust and responsive to configuration changes like screen rotations.
🧩 What is a ViewModel?
The ViewModel
is part of the Jetpack Architecture Components introduced to help developers separate concerns and write cleaner, testable, and scalable code.
Simply put, a ViewModel stores and manages UI-related data that needs to survive configuration changes.
It acts as a communication layer between the UI (Activity/Fragment) and the data layer (Repository, Use Cases, etc.).
💡 Benefits of ViewModel
- ✅ Survives configuration changes like rotation
- ✅ Separates UI logic from business logic
- ✅ Works seamlessly with
LiveData
,StateFlow
, orMutableState
- ✅ Helps in unit testing your business logic without involving Android components
🧑💻 How to Use ViewModel
Let’s look at a simple ViewModel setup in Kotlin:
class UserViewModel : ViewModel() {
private val _username = MutableLiveData<String>()
val username: LiveData<String> = _username
fun updateName(newName: String) {
_username.value = newName
}
}
In your Fragment or Activity:
val viewModel: UserViewModel by viewModels()
viewModel.username.observe(viewLifecycleOwner) { name ->
textView.text = name
}
🔗 Internal Learning Continuity
Here’s how this fits into your ongoing learning:
- Day 1: Android Developer Roadmap
- Day 6: Understanding Android Lifecycle
- Day 10: MVVM Architecture in Android
🧠 Best Practices for ViewModel
Practice | Why It Matters |
---|---|
✅ Use ViewModelProvider | Ensures lifecycle-aware creation |
🚫 Don’t store context | Leads to memory leaks |
✅ Use SavedStateHandle | For restoring state after process death |
✅ Share ViewModel across Fragments | Useful in navigation or tabbed UIs |
🛠 When to Use Shared ViewModel?
A shared ViewModel is useful when multiple fragments under the same activity need to share or react to the same data:
val sharedViewModel: UserViewModel by activityViewModels()
Perfect for bottom navigation, master-detail layouts, or wizard-style screens.
🔍 ViewModel + StateFlow (Modern Approach)
For more modern reactive UIs, use StateFlow
or MutableStateFlow
inside ViewModel:
class UserViewModel : ViewModel() {
private val _userName = MutableStateFlow("Guest")
val userName: StateFlow<String> = _userName
}
Jetpack Compose integrates seamlessly with this approach using collectAsState()
.
Here’s a diagram that visually explains the ViewModel role in Android’s MVVM architecture:

Key Concepts:
- UI Layer: Displays data, observes ViewModel state.
- ViewModel: Acts as the bridge between UI and data, holds logic & data state.
- Repository: Handles data operations, abstracts sources.
- Data Sources: Can be remote (API) or local (Room DB, SharedPreferences).
🎯 Summary
The ViewModel
class is essential in MVVM-based Android apps. It separates UI logic, preserves state, and makes your app resilient and testable. Whether you’re using LiveData
, StateFlow
, or Jetpack Compose, ViewModel is a must-know tool.
👉 Up next: We’ll integrate LiveData
and observe changes in the UI in Day 12: LiveData and StateFlow in Android.