![]() |
VOOZH | about |
ViewModel is a Jetpack Architecture Component used to store and manage UI-related data in a lifecycle-aware manner. It helps retain data during configuration changes such as screen rotation, preventing data loss when an Activity or Fragment is recreated. By separating UI data from UI controllers, ViewModel makes Android applications more stable, maintainable, and easier to manage.
To create a ViewModel, create a separate class that extends the ViewModel class. The Activity or Fragment accesses the ViewModel using ViewModelProvider.
Example:
import androidx.lifecycle.ViewModel
class MainActivityViewModel : ViewModel() {
var number = 0
fun addOne() {
number++
}
}
Android Architecture Components provides the ViewModel helper class for the UI controller that is responsible for preparing data for the UI. ViewModel objects are automatically retained during configuration changes we will see that in the below example. Now let's get into the code,
Step 1: Add these dependencies in the build.gradle file
๐ Imagedependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.8.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.8.0"
}
Also, add the following dependency to the build.gradle(Module:app) file. We are adding these two dependencies because to avoid using findViewById() in our MainActivity.kt file. Try this out otherwise use the normal way like findViewById().
๐ Imageapply plugin: โkotlin-androidโ
android {
buildFeatures {
viewBinding true
}
}
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 3: Working with theMainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file.
Output:
Now just click on the button 3 to 4 times you will see the incremented number on the screen. Now just try to rotate your emulator or device.
You will see the number becomes 0, the question is why? How it erases the value by rotating the screen. Ok to get the answer we have to get some knowledge about the Lifecycle of a ViewModel.
๐ Lifecycle of a ViewModelWhen a configuration change such as screen rotation occurs, the activity is destroyed and recreated, causing its data to be lost. To preserve data across such changes, Android provides ViewModel, which survives configuration changes and retains UI-related data. A ViewModel is typically created in onCreate() and remains available until the activity is permanently finished and destroyed.
Step 1: Create a Kotlin class file MainActivityViewModel.kt. Our MainActivity class file extends the ViewModel class.
Refer to this article:How to Create Classes in Android Studio?
Step 2: Working with theMainActivity.kt file
Go to the MainActivity.kt file and update the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Output:
Even after rotating our screen, we get the same value. So that's it, this is the basic of ViewModel there are many other advanced things of view model we will cover later.