The Android Activity Lifecycle

One thing you need to get used to in Android is how apps are started, paused and destroyed, and the various methods you can call to deal with these states. This is all known as the Activity Lifecycle. Take a look at this diagram:

Activity Lifecycle flowchart

This flowchart shows you the various stages of an Activity, from start to shut down. The grey boxes are all methods that can be called. For example, after the app is started, an Activity is Launched. The first method to be called is the onCreate method, which is the one you've been writing code for.

If you were to press the Home button on an Android device, your app is not destroyed. The onPause and onStop methods are called, and the app goes to sleep; it's there in the background, should you want it again. If you tap the app's icon to restart it, then the onRestart method is called, followed by the onStart and onResume methods. The Activity is then Running again.

However, if you were to press the Back button on an Android advice then you're telling Android that you don't want this Activity anymore. In which case, it's safe to destroy it. So the methods called are onPause, onStop and onDestroy.

Let's get some practice with the Activity Lifecycle. You'll see what happens when a device is rotated.

What we'll do is to increment a variable when a button is clicked. The value of this variable will then be displayed in a Text View. We'll then see what happens when the Android device is rotated.

In the project you created in the previous section, go back to your activity_main.xml file. Click on your First Activity Text View to highlight it. With the Text View highlighted, have a look at the properties area on the right. Locate the ID property and type in mainLabel. Then press the enter key on your keyboard:

The ID property being changed

When you've typed mainLabel, press the Enter key on your keyboard. You may see a message like this one:

The Update Usage message box

Click Yes on the message to rename your TextView. This then sets the ID of the Text View to a new value. We'll be needing a reference to this mainLabel soon.

Now go back to your code. Set up an Integer variable at the top of the code, just under the public class part, and above protected void onCreate.

private int someScore = 0;

We'll increment this variable when the button is clicked.

In your displaySecondActivity button, comment out the code you have. (Two forward slashes before each line, remember.) Your code window should look like this:

Android Code comments

As the first line of code for the displaySecondActivity button, add this:


This just increments the someScore variable.

Next, we need a reference to that Text View, the one with the mainLabel ID. Add this line below the one you have for displaySecondActivity:

TextView myTextView = (TextView) findViewById( );

This gets us a reference to the Text View, just like we did before.

Now we can set the new text for the Text View:

myTextView.setText( String.format("%s", someScore) );

Because someScore is an Integer, we need to convert it to a string.

(Google's advice for its Android operating system is to use String.format to convert your integers to strings, rather than something like Integer.toString(someScore). You can learn more about formatted strings here on our site: Java Formatted Strings)

Your displaySecondActivity code should now look like this:

Converting an Integer to a String in Android

You can try it out, now. Run your app and it will start in portrait view. Click the button a few times and the label should display how many times you have clicked the button:

A value being incremented on an Activity

In the image above, the Text View at the bottom shows that we have clicked the button 5 times.

Now rotate your device. If you are using the Emulator, click the fourth icon down from the ones on the right. You should see this:

The effects of device rotation on an Activity

The number 5 from the Text View has disappeared! Instead, First Activity is back in the Text View. Now click the button again. You should see this:

The onCreate method erasing data when an Activity is rotated

It's gone back to the start, and says we have only clicked the button once. The Text View should have a 6 in it, 5 from before it was rotated and once from after. So what's going on?

When you rotate a device, Android calls the methods onPause, onStop, and then onDestroy. The Activity is then shut down. The Activity in landscape view is then launched, along with the onCreate method again. The variable called someScore is reset to zero. Which is why the button click gives you 1 and not 6.

Android destroys an Activity when the screen is rotated because of something called Device Configuration. Not surprisingly, Device Configuration refers to the various ways you have your phone or tablet configured. Things like screen size, screen density, keyboard layout, etc. If Android needs to grab a new layout for something, it tends to destroy the Activity that's already loaded.

What, then, can you do to solve this? Having data erased every time the device is rotated could be a major headache. Especially if it's someone's highest score ever! You'll see how to solve the problem in the next lesson when we tackle bundles.

Back to the Android Contents Page