Activity Life Cycle (part 2)
LEARNING OBJECTIVES
After this lesson, you will be able to:
- Implement Shared Preferences for saving persistent data
- Describe reasons to use
onPause
andonResume
- Implement freeing system resources using
onPause
andonResume
STUDENT PRE-WORK
Before this lesson, you should already be able to:
- Describe the basic components to the activity life cycle
- Implement saving state on rotation using Bundles
Opening (5 mins)
As we discussed in the last lesson, there are a lot of important steps in the Activity Life Cycle. Now, we are going to concentrate specifically on onPause
and onResume
, exploring why they are useful and examples of when to use them. Also, in the last lesson, we talked about saving state using bundles. To build on that, first, we're going to work on saving persistent data using the Shared Preferences, then, we're going to integrate that into onPause
and onResume
.
Check: Ask students to explain where
onResume
andonPause
fall in the Life Cycle.
Introduction: Persistent Data with SharedPreferences (15 mins)
Sometimes we need the ability to store and retrieve data even when the app is completely closed. Previously, we were storing data in Bundles, which only lasted while the app was running. There are many ways to persist data, such as files and databases. Android provides a built-in utility for this called SharedPreferences
. SharedPreferences
allows you to store any type of data using key-value pairs, where the key is a String, and the value is the data. These preferences remain with the app as long as it is installed.
Check: Ask the students for examples of things you would want to save across app sessions.
One popular use for SharedPreferences
is to hold app settings. For instance, a news aggregator app remembers the type of news articles and the preferred sources you like to receive your news from. You don't want to set this every time, so the app stores this information in SharedPreferences
for easy retrieval the next time the app launches.
Check: Ask the students what SharedPreferences offers over saving state on Bundles.
Demo: Persistent Data with SharedPreferences (10 mins)
Instructor Note: Show the students how to do this in an empty app, just to demonstrate storing and retrieving data.
To start with, we must retrieve the SharedPreferences
Object from Android for your app.
There are two ways to do this. The first is used if you only need to use these SharedPreferences
in this one activity:
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
The second way allows you to provide a key that matches a specific SharedPreferences
file. This key must be unique to your app, such as "com.example.myapp.SOME_KEY_NAME". These preferences, unlike the first, can be retrieved from anywhere in your app:
SharedPreferences sharedPref = context.getSharedPreferences("com.example.myapp.SOME_KEY_NAME",Context.MODE_PRIVATE);
With that done, we are now able to read from SharedPreferences
. To write to SharedPreferences
, we must retrieve an object called an Editor
. Let's look at an example for both of these:
//To read String
String str = sharedPreferences.getString("exampleKey","DEFAULT"); //Initial value of the String is "Hello"
//To save String
Editor editor = sharedPreferences.edit();
editor.putString("exampleKey",str+" World!");
editor.commit();
After committing the changes, the String with key "exampleKey" now contains the value "Hello World!" instead of "Hello".
Check: Ask the students to explain the differences between the two ways of retrieving the SharedPreferences object we discussed.
Guided Practice: Persistent Data with SharedPreferences (10 mins)
In this practice, we will adapt the app from the previous lesson that saved the time the app was launched to use SharedPreferences
instead of a Bundle. In the starter code directory, load the SharedPreferencesBetweenActivities project.
Instructor Note: Be sure to lead this as a discussion, of sorts, probing students about how and why to call and use methods. Solution code is in the appropriate solution code folder.
Check: Was everyone able to complete the app?
Independent Practice: Persistent Data with SharedPreferences (15 mins)
Now it's time to save SharedPreferences
data in one activity and retrieve it in another. We will have an EditText
with a button in the first activity. When the button is pressed, we will be taken to the next activity and whatever was typed into the text box will be shown in the second activity.
Check: Were students able to successfully solve the problem or complete the task?
Instructor Note: Solution code in the appropriate solution code folder.
Introduction: onPause and onResume (15 mins)
As we discussed in the last lesson, there are a number of different methods that are automatically called during the activity creation, pausing, resuming, and destruction processes. Two of those methods are onPause
and onResume
.
onPause
is called when user interaction is being taken away, but the activity is still currently visible (although this could change immediately afterwards)onResume
is called when the activity is visible again and control is being given back to the user
Because these two methods are guaranteed to be called before and after resuming and pausing the activity, respectively, these are the perfect times to perform basic maintenance.
One of the most common uses of these methods is to gain and release system resources. Generally, these are things that can be battery draining (i.e. long running background processes that are only needed while visible, like a streaming video) or system resources that must be acquired (i.e. camera). When resuming, you gain the resource, and when pausing, you release the resource.
Check: Ask the students to tell why we use
onResume
/onPause
instead ofonCreate
/onStop
.
Demo: onPause and onResume (5 mins)
In this demo, we will be looking at when onPause
and onResume
are called in a situation where the screen is only partially covered but the activity is still viewable in the background.
To do this, we will be using a Dialog
, which brings a popup over the screen, causing the underlying activity to pause and resume.
Instructor Note: Starter code can be found in the PauseResumeDemo project. Solution code in the appropriate solution code
Check: Ask the students explain why onStart and onStop were not called.
Guided Practice: Topic (10 mins)
We are going to be making a basic countdown timer app that is only timing while the initial activity is running. When we switch to another activity, we will pause the time until we return to the first activity.
Create a CountDownTimer
using mTimeLeft
and an interval of 1000 milliseconds. The timer has
an onTick
method, an onFinish
method and you can cancel it by calling timer.cancel()
.
Have the timer updare a textview with the time left on each tick, and have it replace the text with the string "done!" when the timer finishes.
@Override
protected void onPause() {
super.onPause();
mCountDownTimer.cancel();
}
@Override
protected void onResume() {
super.onResume();
mCountDownTimer = new CountDownTimer(mTimeLeft, 1000) {
public void onTick(long millisUntilFinished) {
mTimeLeftTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
mTimeLeft = millisUntilFinished;
}
public void onFinish() {
mTimeLeft = 0;
mTimeLeftTextView.setText("done!");
}
}.start();
}
Instructor Note: Starter code can be found in the CountdownApp project. Solution code in the appropriate solution code
Independent Practice: Music Player (15 minutes)
Instructor Note: This can be a pair programming activity or done independently.
You will be modifying the provided music player to pause the music when a new activity is opened and automatically resume it when the user returns to the activity. Starter code is provided in the MusicPlayer project.
The music used in this app is provided by www.bensound.com under the creative commons license
Check: Were students able to create the desired deliverable?
Conclusion (5 mins)
Today we covered SharedPreferences
and how onPause
/onResume
work together to make your app run smoothly. Understanding how the activity life cycle works and how to use it to your advantage will make your apps have a much smoother user experience.