In this chapter, we're going to take a look at the life cycle of an Activity. While it'll seem a bit abstract at first (diagrams!), it's an incredibly important topic that every Android developer gets the hang of. Bear with us.
Description
When your alarm clock rings in the morning, you switch from a sleeping state to an awake state. Then, when you get up and prepare to go to work, you go from an awakened state to an operational state. Throughout the day, you move from one state to another.
On Android, an Activity also passes through different states. Between each state, the system calls specific methods, in a specific order. You already know one of these methods: onCreate().
Here are the different states in which an Activity can be found:
By default, the onCreate() method is automatically overloaded when you create a new Activity. But as you can see from the diagram above, there are others which we will now discover.
onCreate()
This method, which you already know, is called when your Activity is created by the system and enters the Created state.
Generally, we use this opportunity to set up the GUI, initialize variables, configure listeners, and connect to the model.
You may have noticed that this method takes a parameter which is a savedInstanceState variable of type Bundle. This variable contains the last saved status of the Activity. We will make its purpose more clear below.
At this point, the Activity is already created, but the user can't see it, yet and cannot interact with it.
onStart()
This method is called by the system when the Activity enters the Started state. At this point, the interface is visible to the user, but they can't interact with it yet.
onResume()
In the Resumed state, the Activity becomes fully operational. The user can use the application and click on graphic elements. The application remains in this state until something interrupts it, such as receiving a phone call, starting a new Activity, or displaying a dialog box.
onPause()
In the Paused state, anything initiated in onResume()
must be paused in this method. For example, if you start an animation in onResume()
, you should stop it in onPause()
.
The work you do in this method should consume as little time as possible to prevent the user from getting stuck in your Activity.
onStop()
In the Stopped state, the Activity is no longer visible. Code related to GUI updates should be stopped here. Work done in this method can be more mission critical (such as saving values in SharedPreferences, for example).
onDestroy()
This method is called when the Activity is removed from the Activity stack. This method will be called after the finish()
method, or if the system decides to terminate the activity to free up memory.
That's a lot to take in, we know! Let's clarify things by putting this into practice.
Implementation
To understand the sequence of these methods, override them in your two Activities, and display their call in the console. As a reminder, this is how you display a log statement in Java (we will learn how to use the Android-specific one later):
System.out.println("MainActivity :: onStart()");
Launch the application, press the button to start a new game, play it until the end, and wait until you return to the main screen. You should see this in your console:
To see this display, do the following:
Select the Android Monitor tab at the bottom left of Android Studio
Select the name of the emulator or the name of your device, and choose the name of your TopQuiz process from the drop down
Select the logcat tab
Filter the logs by typing System.out in the search box. This will prevent you from seeing all the logs generated by the system
On the MainActivity side, we have:
onCreate()
/onStart()
/onResume()
: the Activity is startingonPause()
: the Activity is paused, because the GameActivity Activity is startingonStop()
: the Activity is stopped because GameActivity Activity is visibleonStart()
/onResume()
: the Activity resumes because GameActivity Activity has finished
As for GameActivity, we have:
onCreate()
/onStart()
/onResume()
: the Activity startedonPause()
/onStop()
/onDestroy()
: the Activity ended and was destroyed
Let's Recap!
In Android, an Activity passes through different states.
The system calls specific methods in a specific order between each state.
By default, the
onCreate()
method is automatically overloaded when you create a new activity.
The more your apps grow, the more of an expert you have to become in managing this cycle, and determining which method you need to use to start and stop your services. And now, follow me to the last chapter of this course to find out what happens when you rotate your device... 😱