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.
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.
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.
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.
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.
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
The work you do in this method should consume as little time as possible to prevent the user from getting stuck in your Activity.
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).
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.
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:
onResume(): the Activity is starting
onPause(): the Activity is paused, because the GameActivity Activity is starting
onStop(): the Activity is stopped because GameActivity Activity is visible
onResume(): the Activity resumes because GameActivity Activity has finished
As for GameActivity, we have:
onResume(): the Activity started
onDestroy(): the Activity ended and was destroyed
Below is a Google diagram which summarizes what we have seen:
Play with the application and analyze the logs to fully understand the sequence of these methods. The more complex your application becomes, the more critical this cycle is to your success.
Make sure you know what each of the following methods do:
The more you work with your application, the more familiar these processes will become!