Android activity life cycle if one of the most asked questions in Android Developer interviews. It is also the one to mess up easily. You must have already seen this activity lifecycle diagram from developer.android.com and many android development books.
We’ll be having two activities: 1. ActivityA(MainActivity) 2. ActivityB and we’ll be exploring the following situations in the activity life cycle.
First create an Android Studio project. In the MainActivity.kt file, add the following methods:
class MainActivity : AppCompatActivity() < var TAG = "AndroidVille" override fun onCreate(savedInstanceState: Bundle?) < super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "Activity A" Log.d(TAG, "Activity A: onCreate" ) override fun onStart() < super .onStart() Log.d(TAG, "Activity A: onStart() " ) override fun onResume() < super .onResume() Log.d(TAG, "Activity A: onResume() " ) override fun onPause() < super .onPause() Log.d(TAG, "Activity A: onPause() " ) override fun onSaveInstanceState(outState: Bundle) < outState.putString( "androidville" , "Hello there" ) super .onSaveInstanceState(outState) Log.d(TAG, "Activity A: onSaveInstanceState() " ) override fun onRestoreInstanceState(savedInstanceState: Bundle) < super .onRestoreInstanceState(savedInstanceState) Log.d(TAG, "Activity A: onRestoreInstanceState() " ) override fun onRestart() < super .onRestart() Log.d(TAG, "Activity A: onRestart() " ) override fun onStop() < super .onStop() Log.d(TAG, "Activity A: onStop() " ) override fun onDestroy() < super .onDestroy() Log.d(TAG, "Activity A: onDestroy() " ) fun launchNewActivity(v: View) < startActivity(Intent( this , ActivityB:: class .java))Now create another activity (ActivityB), and add the same methods, but this time, replace the “ActivityA” text with “ActivityB” in all the log statements.
package com.example.androidactivitylifecycle import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log class ActivityB : AppCompatActivity() < var TAG = "AndroidVille" override fun onCreate(savedInstanceState: Bundle?) < super .onCreate(savedInstanceState) setContentView(R.layout.activity_b) title = "Activity B" Log.d(TAG, "Activity B: onCreate" ) override fun onStart() < super .onStart() Log.d(TAG, "Activity B: onStart() " ) override fun onResume() < super .onResume() Log.d(TAG, "Activity B: onResume() " ) override fun onPause() < super .onPause() Log.d(TAG, "Activity B: onPause() " ) override fun onSaveInstanceState(outState: Bundle) < outState.putString( "androidville" , "Hello there" ) super .onSaveInstanceState(outState) Log.d(TAG, "Activity B: onSaveInstanceState() " ) override fun onRestoreInstanceState(savedInstanceState: Bundle) < super .onRestoreInstanceState(savedInstanceState) Log.d(TAG, "Activity B: onRestoreInstanceState() " ) override fun onRestart() < super .onRestart() Log.d(TAG, "Activity B: onRestart() " ) override fun onStop() < super .onStop() Log.d(TAG, "Activity B: onStop() " ) override fun onDestroy() < super .onDestroy() Log.d(TAG, "Activity B: onDestroy() " )Now, we’ll create a Log Filter to just focus on our custom log statements. For this,
This will help you filter out the unnecessary log statements and focus on the activity life cycle ones.
With this setup, we’re ready to explore the activity life cycle method’s call sequence. Go ahead and run the app, make sure your logcat window is opened up and clean.
When the app launches, this is the callback sequence:
First, the ActivityA’s onCreate method is called, which sets up the layout of the activity. Then we have onStart which is the place to set up things such as broadcast receivers. And finally onResume when Activity is interactive.
This is the most commonly asked question about android activity life cycle. What are the callback methods when one activity is launched above another.
I cannot stress enough the importance of this question and also one that people can get wrong. Because it’s so simple, I used to commit this mistake of saying all methods of ActivityA and then B.
Let’s see what happens here:
This is the activity life cycle callback sequence:
First, the ActivityA is paused, then life cycle methods of ActivityB are called. But finally the ActivityA is stopped.
Important point to note here is that ActivityA is stopped (not destroyed) after ActivityB has been created and is interactive.
Now when pressing back from ActivityB we land on ActivityA and this is the callback sequence:
Here we see that at the end, ActivityB is destroyed. Notice that when launching B on A, ActivityA wasn’t destroyed but stopped, and it’s process lived on.
Let’s see what happens when we press the back button from ActivityA (MainActivity).
Since ActivityA is the last activity, on pressing back button, the app exits and the process of ActivityA is destroyed.
This is another important scenario in these questions. And this one is a bit unique, which makes use of onRestoreInstanceState. We’ll be rotating the device from ActivityA.
Here are the callback method call sequence:
You can get the key value pairs you store from onSaveInstanceState, inside onRestoreInstanceState.
On pressing home button from ActivityA, you’ll get the following callback sequence:
And when you come back from paused state (by pressing recents button on android), following is the sequence:
So these were some of the scenarios in Android Activity Life Cycle. Let me know if you want me to post some other scenarios as well. In the upcoming posts, I’ll try to make one with fragments lifecycle and how they work with activity life cycle.
Published on Java Code Geeks with permission by Ayusch Jain, partner at our JCG program. See the original article here: Android Activity Life Cycle – The Complete Guide
Opinions expressed by Java Code Geeks contributors are their own.