![](/book_covers/jetpack_compose_front_cover_150x190.png)
Jetpack Compose Essentials was published on January 28, 2022. The current revision is 1.0. The revision of your copy of the book can be found in the copyright page at the start of the book.
Chapter 40 - A Compose ViewModel Tutorial
If you are using the latest version of Jetpack Compose (either 1.1.x or 1.2.x), you will need to make the following changes before running the ViewModelDemo app:
1. Change the DemoViewModel declaration so that it subclasses from ViewModel() as follows:
. . import androidx.lifecycle.ViewModel . . class DemoViewModel : ViewModel() { var isFahrenheit by mutableStateOf(true) var result by mutableStateOf("") fun convertTemp(temp: String) { . .2. Add the following library dependency to the module level build.gradle file:
dependencies { . . implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1' . .3. Obtain a ViewModel instance in the MainActivity.kt file via a call to the viewModel() lifecycle function. This will return either an existing view model instance, or create a new one if no previous instance exists. This function also ensures that the view model is retained until the activity is destroyed or the app exits:
. . import androidx.lifecycle.viewmodel.compose.viewModel . . @Composable fun ScreenSetup(viewModel: DemoViewModel = viewModel()) { MainScreen( . .
Chapter 43 - A Compose Database and Repository Tutorial
If you are using the latest version of Jetpack Compose (either 1.1.x or 1.2.x), please follow the steps in the updated chapter on-line here.
If you have encountered an issue with the book not listed above please contact us as at [email protected] and we will work to resolve the issue for you as quickly as possible.