Architecture component — Introducing ViewModel
In this article, I will talk about ViewModel — A class that was introduced through Architecture component.
What is ViewModel?
ViewModel is a class that belongs to the androidx.lifecycle
package, it is designed to hold UI-related data needed for the view (activities, fragments) it attaches to, ViewModel is lifecycle aware, and what makes it special is that it can survive from a configuration change such as rotation of the phone screen, this capability saves developers from implementing logic to save and restore view states in such situations.
Why ViewModel?
As mentioned, ViewModel is designed to be lifecycle conscious and can survive from a configuration change, it becomes very useful for developers to build MVVM pattern. Compared to MVP pattern these are the benefits of adapting ViewModels:
- Lifecycle-aware: Compared to MVP pattern where you typically need to attach your presenters to your views by passing reference of a view to a presenter when the view has STARTED and to detach from the view when it enters the STOP state. You do not need to worry about this with ViewModels.
- Survives from configuration changes: ViewModel is designed to survive from a configuration change this will make our life easier while handling view state restoration in situations like screen rotation. Note, ViewModels only survive configuration change-related destruction they do not survive from the process of being stopped.
- ViewModel is able to store large complex data: Compared to
onSaveInstanceState()
callback ViewModel is capable of holding on to large and complex data. - Communication channel for fragments: One of the challenges in developing an Android app with fragments is to share data between two fragments within an Activity, ViewModel allows you to achieve this easily using the
by activityViewModels()
Kotlin property delegate. - Native support for coroutines: ViewModel works well with Coroutines really well, as it supplies a `viewModelScope: CoroutineScope` out of the box, the scope will be canceled when ViewModel gets
cleared
.
How to use ViewModel ?
To use ViewModel, you need to follow these steps:
Step 1. Import ViewModel library into your project
Add the gradle library into your build.gradle
for your project or module:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
Step 2. Create your first ViewModel class
Create your ViewModel class by subclassing the ViewModel
base class.
class MyViewModel: ViewModel() { ....
}
Step 3. Initialize the ViewModel and attach it to your LifecycleOwner
After defining your ViewModel class, you need to initialize and attach the instance to a lifecycle owner, you can achieve that with the code below:
ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(MyViewModel::class.java)
Things to note
ViewModel is a great way to simplify your code and implement complex features with ease. There are few things to note while using ViewModel to avoid memory overflow and improve your code testablity:
⚠️ Do not pass reference of Views to your ViewModel.
Since ViewModel can survive from configuration change, your ViewModel class should not hold any reference to your views, activities, fragments etc, as these instances will be destroyed and recreated during the process and you will end up holding on to instances that are no longer existed, in some cases this might also lead to memory leaks.
⚠️ Dont let ViewModel know about Android framework.
Its recommended to avoid having Android framework classes in your ViewModel, this is to improve your code testability and modularity.