Using LiveData Transformations

Now that CrimeFragment has the crime ID, it needs to pull the crime object from the database so it can display the crime’s data. Since this requires a database lookup that you do not want to repeat unnecessarily on rotation, add a CrimeDetailViewModel to manage the database query.

When CrimeFragment requests to load a crime with a given ID, its CrimeDetailViewModel should kick off a getCrime(UUID) database request. When the request completes, CrimeDetailViewModel should notify CrimeFragment and pass along the crime object that resulted from the query.

Create a new class named CrimeDetailViewModel and expose a LiveData property to store and publish the Crime pulled from the database. Use LiveData to implement ...

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.