Debunking Android Activity-Lifecycle.

Abhishek Gururani
4 min readJul 2, 2021

--

Say hi.

When you leave your house you do not leave the door open(sorry Bruno), you lock the door and depending upon the situation you put the keys, either with you or below the “WELCOME” mat.
Now think of it this way : imagine your house as an activity and you are a programmer, so initially when you were inside your house, your house was in state 1, then when you went away it was in state 2. When you were not in your house(state 2) you made some arrangements(like you placed the keys under the mat).

This is the case with android activity as well, it goes through a cycle of different states and to make your app more robust you need to make some
arrangements before/after/on each transition. Developer.android.com defines an activity as a single, focused thing that a user can interact with, think of it as a single UI screen with which the user can interact. When an activity is in action it can go through various stages and for a beginner it may get weary to understand those states.
I guess we are on the same page so now let’s debunk this crazy concept called Activity-lifecycle. When I was starting out with the basics of android development, just like you I googled for ‘Activity-Lifecycle android’ and the results were crazy. Every single one of them started out with the Activity Lifecycle flowchart, and it was confusing so let’s not start with that.

Let’s directly talk about different states and their respective callback methods. Similar to the main() function of every C language program which acts as the entry point when the program runs in a hosted execution environment, there are also various functions(callback methods) that correspond to specific stages of Activity Lifecycle. According to android.developer.com the Activity class provides 6 prime callback methods: onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy().

Instead of blabbering theory, let’s directly write a dummy program and see which callback is executed when:

<so I’ve created a simple Android program that greets the user with a “Hello World!”, and used Log class that allows to create log messages that appear in Logcat pane, if you want me to write a tutorial on that do let me know>

Let’s launch the application and see what appears in our Logcat :

Application launched.

Hmm, we can clearly see onCreate() being called, this is the first callback that’s called when an application is launched for the first time. This marks the Created state of our activity, It sets the view and if implemented in a particular fashion it may also recover the instance state(if available). After that we can see onStart(), which is invoked when the activity enters the Started state. From now on till the end of Paused state our activity is in the visible state.

putting application on the background.

Now I pressed the home screen button, and my application has moved to the multitask bucket. As you can see in the Logcat, onPause() was invoked which means my activity first went to Paused state after that onStop was called which is invoked when the activity enters Stopped state which also means that now my activity is no more visible.

Here you can also see onSaveInstanceState() being called, As your activity begins to stop, the system calls this method so your activity can save state information to an instance state bundle. This bundle can be used in onCreate() method to restore the UI of previous state of our activity.

now from Stopped state, our activity can take either ways:

  1. Comes back to the user, or
  2. Gets terminated, let’s review both of them:
Bringing the activity back to the foreground.

Here the activity is brought back to the foreground, you can clearly see onRestart() being invoked followed by onStart() and onResume(), onRestart() is called if the activity is re-displayed to the user(the user navigates back to the activity).

Terminating our beloved activity, Good-Bye World!

Finally the activity is terminated and you can see the transition of our activity Paused state/onPause() -> Stopped state/onStop() -> (onStateInstanceState, saving state information) -> Destroyed state/onDestroy().

Now go through this Activity-Lifecycle flowchart and you’ll understand all the transitions we just talked about.

Thanks, if this was helpful remember me in your prayers. Or else feel free to criticize.

To make the story preview more sassy, ignore.

--

--

Abhishek Gururani

I write articles on Android Development, and problem solving.