A Tiny Intro to the Android Activity Lifecycle
The following is a tutorial on the Android Activity Lifecycle, a concept that occurs pretty often during mobile dev interviews
There is also an app to demo the topic, simple, a decent way to go through the cycles and process. It is one buck for the support but also open sourced on Github for people who like to learn with examples and more logs (it can be easily tested on Android Studio).
Before going trough the lifecycle process and explaining the main parts, let’s see what is an Android Activity. An activity is a view, a window with related design and interactions. The following is a single activity that has 2 buttons and some text describing in which state of the Activity Lifecycle the app is.
This activity, as any Android Activity, goes through the following states all along its lifecycle:
The onCreate() statement is called anytime there is a new instance of the activity created. This means that for any subsequent starts of the activity, the onCreate() statement is not called because the activity was already created/loaded. The onCreate() statement is called when…
- the user loads a new activity for the first time
- the user exits the current view by hitting the Back button and then returns to it. That’s because when the user presses the Back button, the current activity is destroyed and once the user returns to it, it has to be created again.
The onStart() statement is called, you guessed it, when any instance of the activity starts : when the instance loads for the first time, but also when there are any subsequents reloads, basically anytime an activity shows up to the foreground. This is the state when the application becomes visible to the user.
The onResume() statement is called when the app activity becomes ready to respond to the user, which means anytime except when the activity is onPause(). Let’s go to the next step in order to have a better understanding of onResume/onPause().
We can test the onResume() action by pressing the Back button once in the onPause() state (click on the Checkmark button to pause Main Activity in the Activity Lifecycle app).
If the activity is partially visible but somehow not in focus, the activity is onPause(). It’s the case with the next example where we have a transparent second activity that partially obstructs our main activity. We can test the onPause() action by pressing the Checkmark button in the Activity Lifecycle app.
We can stop the paused activity by sending it to the background and come back to it by bringing it back to the foreground: it will still stay in the onPause() state. We can test this by pressing either the Circle button or the Square button of the device while the application is paused, switching to another view from a different app, and then coming back to our Activity Lifecycle app.
In order to better understand the onPause()/onStop() difference, let’s have a look at how onStop() works.
The onStop() statement is always called when the activity is in the background. But what’s the difference between onPause() and onStop() ? onPause() is the equivalent of a car stoping at a red light. It doesn’t turn off the motor, it still goes on, still driving, but with a lot less resource consumption. That’s different comparatively to a situation where the car is stopped at a grocery store (the motor is turned off), sending the activity to the background (not driving anymore). You can experiment onStop by pressing either the Square button or the Circle button.
The onRestart() statement is always called when the activity is about to be displayed on the foreground after being stopped.
The onDestroy() statement is always called when the application is killed, either by the user or by the operating system because it’s low on resources.
One last thing
Now what if we want to run a component in the background (say play music) even if the activity is stopped? It is called a service. A service runs in the background without any interaction with the user except the one starting the service and stopping the service. It can act independently of any activity lifecycle, processing continuously in the background. A MediaPlayer for example is a service.
In the Activity Lifecycle app, we can demo the service utility by pressing the Music button, sending the app to the background by pressing the Circle button and noticing that the music is still playing.
You can press Ctrl + Fn + F6 on a Mac or Ctrl + F6 on a PC to increase the volume.
A service is stopped when the process starting it is destroyed (the app is killed) or when the service is stopped (in the Activity Lifecycle app, press the Music button one more time).