What is Activity Lifecycle in Android — Explain with App Demo

Before going to see what is the activity lifecycle, first discuss what is Activity in android. An activity is a single screen user interface. This the entry point of the users. Activity is the subclass of the ContextThemeWrapper class. The Activity runs with the help of a core set of six callbacks.

These all callbacks provide a lifecycle of activities that helps to know what is going on in an activity. These callbacks allow the app to know that the stage has changed.

Graphical Representation of Activity-Lifecycle
Graphical Representation of Activity-Lifecycle

Let’s take an example to know it a better way: when we create a streaming video player, we need to add functionalities like play the video, pause the video, resume the video, pause the video and terminate the network, then again reconnect the network and play the video, back from the video and play another video, and close the app. These all are the activity lifecycle and all of these stages have to perform under activity callbacks

Here, start the app falls under onCreate() callback method, play the video falls under onStart() callback method, streaming the video falls under onResume() callback method, pause the video falls under onPause() callback method, then again play the video navigated the user to the onResume() callback method, back from the current video falls under onStop() callback method, and at last close, the app falls under onDestroy().

Concepts of Activity-Lifecycle

Let’s discuss the concept and implementation of the core six callback methods of the activity lifecycle.

  • onCreate()

When we first create the activity, the activity enters the Create state. In the onCreate() method, the basic creation of information is stored. When the activity is created for the first time then the onCreate() method is called. The onCreate() method takes the parameter savedInstanceState, which is a Bundle object that stored the previously saved state of the activity. If the activity has never been before, the value of the Bundle object is null.

After executing the onCreate() method, the system enters to the onStart() and onResume() methods in quick succession.

** onCreate() method call only once throughout the whole activity-lifecycle.

  • onStart()

After executing the onCreate() method, the activity enters the onStart() method. The onStart() method makes the activity visible to the user, it enters the foreground and makes interactive for the users.

The onCreate() method is very quick, after executing the onCreate() method, the system enters the onResume() method and the activity enters the Resumed state.

  • onResume()

after the onStart() method system enter the onResume() method. When the activity is in the Resumed state, it comes to the foreground and the system entered the onResume() method. In this state, the users interact with the app and the app stays in the same state until the users perform any other activity.

When the user performs any other activity like move to another screen or receiving any phone call or receiving any text message, the system automatically enters the onPause() method, and the activity becomes invisible to the users. It is in the Paused state.

  • onPause()

When the system invoked the onPause() method, it means that the activity is not in the foreground, the user is leaving the activity. Use the onPause() method to pause the actions that should not perform while the Activity is in the Paused state, and that expects to resume soon.

If the activity becomes completely invisible to the users, the system calls onStop().

  • onStop()

When the activity is no longer visible to the user, it has entered the Stopped state, and the system invoked the onStop() callback. In the onStop() method, the app adjusts the resources that are not needed to the user while it is in the stopped state.

If the activity becomes visible once again, the system invokes the onRestart() callback method or if the Activity is finished running, the system calls onDestroy() callback method.

  • onDestroy()

The onDestroy() is called before the activity is destroyed. When the activity is finished or destroyed by the system then onDestroy() callback method invoked.

  • * onDestroy() method also called only once throughout the whole activity-lifecycle.

Activity-Lifecycle Example, for demo app with activity lifecycle explanation, visit https://www.gbandroidblogs.com/2020/07/what-is-activity-lifecycle-in-android-explain-with-app-demo.html

Thank you!