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.

Screen Shot 2015-11-03 at 5.34.07 PM.png

This activity, as any Android Activity, goes through the following states all along its lifecycle:

lifecycle.png

1) onCreate()

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…

2) onStart()

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.

3) onResume()

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).

4) onPause()

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.

Screen Shot 2015-11-03 at 9.54.59 PM.png

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.

Screen Shot 2015-11-03 at 9.59.19 PM.png

In order to better understand the onPause()/onStop() difference, let’s have a look at how onStop() works.

5) onStop()

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.

6) onRestart()

The onRestart() statement is always called when the activity is about to be displayed on the foreground after being stopped.

7) onDestroy()

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).

@dandancrisan

Mobile app

Source code

 
12
Kudos
 
12
Kudos

Now read this

A Tiny Intro to Publishing Your Android App on Google Play

The following is a simple technical guide to help developers to publish their Android app on Google Play. Here is what you need : A Google Play publisher account (which is mainly an Android developper account). You can sign up from here... Continue →