Adding a New Fragment and ViewModel

The first step is to add a new ViewModel to store the List of Crime objects you will eventually display on the screen. As you learned in Chapter 4, the ViewModel class is part of the lifecycle-extensions library. So begin by adding the lifecycle-extensions dependency to your app/build.gradle file.

Listing 9.1  Adding lifecycle-extensions dependency (app/build.gradle)

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
    implementation 'androidx.core:core-ktx:1.1.0-alpha04'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

Do not forget to sync your Gradle files after making this change.

Next, create a new Kotlin class called CrimeListViewModel. Update ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.