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.
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.
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.
** onCreate() method call only once throughout the whole activity-lifecycle.
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.
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.
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().
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.
- * 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