Adding a New Fragment and ViewModel

The first step is to add a 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-viewmodel-ktx library. So begin by adding the lifecycle-viewmodel-ktx dependency to your app/build.gradle file (that is, the build.gradle file labeled Module: CriminalIntent.app).

Listing 10.1  Adding lifecycle-viewmodel-ktx dependency (app/build.gradle)

dependencies {
    ...
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.fragment:fragment-ktx:1.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    ...
}

Do not forget to sync your Gradle files after ...

Get Android Programming: The Big Nerd Ranch Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.