Through out this article, we are going to understand the Activity Lifecycle in Android.

In Android, the Activity is one of the most crucial components. Although we have discussed in our previous article, what the activity is. Are you are a new guy in Android App Development? If so, you should read that article first. Because today we will be focusing on the Activities lifecycle only.

The Activity class or AppCompatActivity class already provides several callbacks. And using those callback methods that activity then gets to know, what the state it is at now. Therefore when a user leaves or re-enters the activity, we can do certain changes in the activity class in order to handle our app’s behaviors. For instance, you have a video player app. Also, you are watching a video over there. And now suddenly you want to leave the app for a moment. After some time, you re-open the app and playing that same video. You will see most of the apps are actually start playing that video from the beginning, instead what we can do is, simply we can redirect to that particular duration. For that, we just need to save the last duration and the name of the video or path when the user leaves the app. So simple! Right!


The Activity Lifecycle Diagram

There are a total of seven callbacks onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy() and onRestart(). From the below diagram, you can see at the very beginning onCreate() method is placed. So this is the method through which we enters into the activity.

Android Activity Lifecycle Diagram

In the end, the onDestroy method gets called and the activity is no more available or completely shut down by the system. Anyway, Let’s explore all the callbacks one by one.

onCreate()

At the very beginning, this method will be called when all the views or activities are created. It also has a bundle object called savedInstanceState to get the previous state of the activity.

onStart()

This callback follows the onCreate method. After visible the activity. This method also called after onRestart() callback. When the user navigates to the activity.

onResume()

After onStart callback, the onResume method will be called. Almost every time whenever a user creates or restarts the activity.

onPause()

Before the onStop method, this one will be called. When the activity loses visibility and enters into the previous activity.

onStop()

Every time after onPause, this onStop method will be called. On that time activity will not be visible.

onDestroy()

Before destroying the activity, onStop is called. And this is the final callback.

onRestart()

After onStop, this onRestart method will be called. And we can restart the activity without calling onCreate method.


Activity Lifecycle Uses

Now we will take you through the android activity lifecycle example. So let’s have a closer look at how and when those lifecycle callbacks are actually called. But before that, we have to override all the callbacks inside an activity. Then inside each callback simply write a Log message. As shown in the below code snippets.

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "DailyCoding";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: ");
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ");
    }
 
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
    }
 
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart: ");
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
}

Anyway, these below use-cases are important. So try to understand each use-case carefully.




#Use-Case 01: Open an Activity and press the Back button

Android lifecycle for activity
Use-Case 01: Open the App and Press the Back button

From the above animation, when you first time opens an app or activity, onCreate, onStart and onResume callbacks get triggered. As shown in the activity lifecycle diagram.

Now onPause, onStop and onDestroy callbacks have triggered, as soon as we press the back button.


#Use-Case 02: Open an activity and press Overview button

Android lifecycle for activity
Use-Case 02: Press the Square Button or Overview Button

As we know, onCreate, onStart and onResume methods will be called. But only if we create an activity. Now let’s press the Overview button, you can see onPause and onStop callbacks have been triggered immediately. If we re-enter the activity, onRestart, onStart and onResume methods have been triggered.

Again press the Overview button and swipe towards the top, left or right to close the app. And we can see this time onPause, onStop and onDestroy callbacks have been triggered.




#Use-Case 03: Open an activity and press Home button

Android lifecycle for activity
Use-Case 03: Press the Home Button

As of now, we know which callbacks will be triggered while activity is becoming alive. So now if we press the Home button, our app will be minimized. But it will not be killed. So rather than onDestroy method call, onPause and onStop will be called.

Remember unless the system terminates our app or activity, the onDestroy method will not be called. So the onDestroy method will be called only before terminating the activity.


#Use-Case 04: Open an Activity and Lock the Screen

activity lifecycle while screen lock
Use-Case 04: Sreen Lock

In this use-case, if we simply turn off the screen while using the app, onPause and onStop methods are called. As shown in the above animation.

Now unlock the screen. You can see onRestart, onStart and onResume callbacks are called.




#Use-Case 05: Open an Activity and Phone Ringing

Android lifecycle for activity
Use-Case 05: Phone Ringing

Let’s take another situation. Although, all the lifecycle callbacks are the same as shown in Use-Case 04. Now if an incoming call comes while running activity and if we receive that call, in this case, onPause and onStop callbacks will be triggered.

If we disconnect the call, onRestart, onStart and again onResume callbacks will be triggered.



Conclusion And What Next?

After all, we’ve learned Activity Lifecycle. How it works. Especially for now just try to remember those 5 Use-Cases. That’s it. If you have any questions leave them in the comment section or on #DailyCoding.

The next topic will be the Android Manifest File. So stay tuned and keep reading articles.

I hope you got all the points of Android AutoCompleteTextView. Anyway if you liked this article then please Subscribe to our Youtube channel for keeping this talk continue. You can also find us on Facebook and Instagram. Thanks for your support.


References:



Translate »