44.4 Adding Saved State Support to the ViewModelDemo Project
With the basics of ViewModel Saved State covered, the ViewModelDemo app can be extended to include this support. Begin by loading the ViewModelDemo_LiveData project created in “An Android Jetpack LiveData Tutorial” into Android Studio (a copy of the project is also available in the sample code download), opening the build.gradle (Module: ViewModelDemo.app) file and adding the Saved State library dependencies (checking, as always, if more recent library versions are available):
.
.
dependencies {
.
.
implementation "androidx.savedstate:savedstate:1.1.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.1"
.
.
}
Next, modify the MainViewModel.kt file so that the constructor accepts a SavedStateHandle instance. Also import androidx.lifecycle.SavedStateHandle, declare a key string constant...