ViewModels : A Simple Example
Introduction
A little over two years ago, I was working on Android for Beginners; a class that takes students from zero programming to their first Android app. As part of the course, students build a very simple one screen app called Court-Counter.
Court-Counter is a very straightforward app with buttons that modify a basketball score. The finished app has a bug though; if you rotate the phone, your current score will inexplicably disappear.

What’s going on? Rotating a device is one of a few configuration changes that an app can go through during its lifetime, including keyboard availability and changing the device’s language. All of these configuration changes cause the Activity to be torn down and recreated.
This behavior allows us to do things like use a landscape orientation specific layout when the device is rotated on its’ side. Unfortunately it can be a headache for new (and sometimes not so new) engineers to wrap their head around.
At Google I/O 2017, the Android Framework team introduced a new set of Architecture Components, one of which deals with this exact rotation issue.
The ViewModel class is designed to hold and manage UI-related data in a life-cycle conscious way. This allows data to survive configuration changes such as screen rotations.
This post is the first in a series exploring the ins and outs of ViewModel. In this post I’ll:
- Explain the basic need ViewModels fulfill
- Solve the rotation issue by changing the Court-Counter code to use a ViewModel
- Take a closer look at ViewModel and UI Component association
The underlying problem
The underlying challenge is that the Android Activity lifecycle has a lot of states and a single Activity might cycle through these different states many times due to configuration changes.

As an Activity is going through all of these states, you also might have transient UI data that you need to keep in memory. I’m going to define transient UI data as data needed for the UI. Examples include data the user enters, data generated during runtime, or data loaded from a database. This data could be bitmap images, a list of objects needed for a RecyclerView or, in this case, a basketball score.
Previously, you might have used onRetainNonConfigurationInstance to save this data during a configuration change and unpack it on the other end. But wouldn’t it be swell if your data didn’t need to know or manage what lifecycle state the Activity is in? Instead of having a variable like scoreTeamA within the Activity, and therefore tied to all the whims of the Activity lifecycle, what if that data was stored somewhere else, outside of the Activity? This is the purpose of the ViewModel class.
In the diagram below, you can see the lifecycle of an Activity which undergoes a rotation and then is finally finished. The lifetime of the ViewModel is shown next to the associated Activity lifecycle. Note that ViewModels can be easily used with both Fragments and Activities, which I’ll call UI controllers. This example focuses on Activities.

The ViewModel exists from when the you first request a ViewModel (usually in the onCreate the Activity) until the Activity is finished and destroyed. onCreate may be called several times during the life of an Activity, such as when the app is rotated, but the ViewModel survives throughout.
A very simple example
There are three steps to setting up and using a ViewModel:
- Separate out your data from your UI controller by creating a class that extends ViewModel
- Set up communications between your ViewModel and your UI controller
- Use your ViewModel in your UI controller
Step 1: Create a ViewModel class
Note: To create a ViewModel, you’ll first need to add the correct lifecycle dependency. See how here.
In general, you’ll make a ViewModel class for each screen in your app. This ViewModel class will hold all of the data associated with the screen and have getters and setters for the stored data. This separates the code to display the UI, which is implemented in your Activities and Fragments, from your data, which now lives in the ViewModel. So, let’s create a ViewModel class for the one screen in Court-Counter:





