May 2022
Intermediate to advanced
688 pages
20h 12m
English
You are ready to create your ViewModel subclass, QuizViewModel. In the project tool window, right-click the com.bignerdranch.android.geoquiz package and select New → Kotlin Class/File. Enter QuizViewModel for the name and double-click Class in the list of options below.
In QuizViewModel.kt, add an init block and override onCleared().
Log the creation and destruction of the QuizViewModel instance, as shown in Listing 4.2.
Listing 4.2 Creating a ViewModel class (QuizViewModel.kt)
private const val TAG = "QuizViewModel"
class QuizViewModel : ViewModel() {
init {
Log.d(TAG, "ViewModel instance created")
}
override fun onCleared() {
super.onCleared()
Log.d(TAG, "ViewModel instance about to be destroyed")
}
}
Read now
Unlock full access