Android Activity Lifecycle Tips & Tricks — Part 1

Avijit Karmakar
5 min readJan 10, 2018

Firstly, I wish you all a very Happy New Year 11111100010.

“May you all create so many Open Source projects and push it to Github and also create so many useful and interactive Android apps that solve our daily problems and needs.”

In this article, I am going to discuss with you regarding the Activity Lifecycle. I will discuss it in several parts.

Android Activity Lifecycle Tips & Tricks — Part 1 (You are here)

Android Activity Lifecycle Tips & Tricks — Part 2

Android Activity Lifecycle Tips & Tricks — Part 3

Android Activity Lifecycle Tips & Tricks — Part 4

First I will discuss what’s the Activity in brief?

Activity is a very important component in Android. An Activity is actually a single screen where we can see the view or interact with the app. Normally, an app consists of so many activities if the app is a multi-screen app.

Activity Lifecycle:

Now moving to the Activity Lifecycle. Every process has its own lifecycle. Just like that an Activity also has its own predefined lifecycle. Activity provides us some lifecycle method callbacks by which we can get to know the state of the Activity has changed. These lifecycle methods are called by the system when an Activity is created, started, resumed, paused, stopped or destroyed.

Activity Lifecycle Methods:

  1. onCreate(): This is the first method of the activity lifecycle callback which was called when an Activity is created. We render the UI of the activity, initialize the UI components and other resources here.
  2. onStart(): This method gets called when the activity has started after creating. This method is called after onCreate() method if the Activity is just created or it can be called after onRestart() method if the Activity is restarting again from stopped state.
  3. onResume(): This method gets called when the activity is in the resumed state. This method is called after onStart() method if the Activity has started or it can be called when the app is fully visible after paused state means after onPause() method. Here the UI of the activity is visible and interactive to the user.
  4. onPause(): This method gets called when the UI is partially visible to the user. If a dialog is opened on the activity then the activity goes to pause state and calls onPause() method. Here the activity is in the paused state.
  5. onStop(): This method gets called when the UI is not visible to the user. Then the app goes to stopped state.
  6. onDestroy(): This method gets called before the Activity has destroyed.
  7. onRestart(): This method gets called when a stopped Activity is restarted from the stopped state. So, this method always gets called after the onStop() method.
  8. onSaveInstanceState(): This method gets called after onPause() and before onStop() of an Activity gets called. If we have to maintain the state of an activity when the orientation is changed then we have to store the data in a bundle by some key-value pairs within this method. We must call the super implementation of this method to save the state of the view hierarchy.
  9. onRestoreInstanceState(): This method is used to restore the view of an Activity. If we saved the state of an Activity in onSaveInstanceState() method and after that the Activity is destroyed and recreated again then we will get the previous instance data of the Activity within the Bundle in onCreate() and onRestoreInstanceState() methods. Now, we have to fetch the data from Bundle and set those data in the view. We must call the super implementation of this method to restore the state of the view hierarchy.
  10. Also we have some other methods like: onPostCreate(), onPostResume() etc. These methods are mainly used for the Activity’s internal purpose but we can override those methods and can write our own code.

Please check the image below. I think you have already seen this image.

Activity Lifecycle

But there are some scenarios which we have to take a look in depth. Now I am going to discuss those scenarios.

I have created a Github repository named ActivityLifecycle where you can get the codebase to test the Activity Lifecycle. I have added some logs and toasts in every Activity Lifecycle callback methods to check which methods get called sequentially. This is the Github link to the repository: https://github.com/AvijitKarmakar/ActivityLifecycle

Scenario 1: Start an Activity, Press the Home Button and Open the App again

When we open an Activity, at that time the Activity’s onCreate(), onStart() and onResume() is get called sequentially. After that, the Activity is visible and interactive to the user.

I/com.ak.apps.activitylifecycle.MainActivity: onCreate()
I/com.ak.apps.activitylifecycle.MainActivity: onStart()
I/com.ak.apps.activitylifecycle.MainActivity: onPostCreate()
I/com.ak.apps.activitylifecycle.MainActivity: onResume()
I/com.ak.apps.activitylifecycle.MainActivity: onPostResume()

Now if we press the Home button then the Activity first calls the onPause() method and then onStop() method get called as the activity is not visible to the user anymore.

I/com.ak.apps.activitylifecycle.MainActivity: onPause()
I/com.ak.apps.activitylifecycle.MainActivity: onSaveInstanceState()
I/com.ak.apps.activitylifecycle.MainActivity: onStop()

If we open the app again then the onRestart() method get called as the Activity is in the stopped state. After onRestart(), onStart() and onResume() methods get called and the Activity is visible and interactive to the user.

I/com.ak.apps.activitylifecycle.MainActivity: onRestart()
I/com.ak.apps.activitylifecycle.MainActivity: onStart()
I/com.ak.apps.activitylifecycle.MainActivity: onResume()
I/com.ak.apps.activitylifecycle.MainActivity: onPostResume()
Activity Lifecycle Diagram Scenario 1

You can also check the video given below.

Activity Lifecycle Video Scenario 1

Scenario 2: Call finish() method in onCreate() method

While opening an Activity, onCreate() method of that Activity gets called. Within this onCreate() method if we call finish() method then it will execute the whole onCreate() method first and then it will execute the lifecycle method onDestroy() and the Activity gets destroyed.

I/com.ak.apps.activitylifecycle.MainActivity: onCreate()
I/com.ak.apps.activitylifecycle.MainActivity: onDestroy()

In the Github repo, you can see that the finish() method written before a log and a toast. If the finish() method executed in the lifecycle method onCreate() then it does not call the lifecycle method onDestroy() instantly. It completes the execution of onCreate() method then onDestroy() method gets called.

Activity Lifecycle Diagram Scenario 2

You can also check the video given below.

Activity Lifecycle Video Scenario 2

In the next parts of this blog, I will discuss more scenarios regarding the Activity Lifecycle within a Single Activity or Multiple Activities.

Thanks for reading this article. Be sure to click 👏 below to applause this article if you found it helpful. It means a lot to me.

To get more programming related articles, follow me so that you’ll get notified when I write new posts.

Also, Let’s get connected over LinkedIn, Github, Facebook and Twitter.

--

--