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 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.