Android – Understanding Android Activity Lifecycle

Hello Readers, CoolMonkTechie heartily welcomes you in this article (Understanding Android Activity Lifecycle).

In this article, We will learn about Activity Lifecycle in android. Activity in Android is one of the most important components of Android. It is the Activity where we put the UI of our application. So, if we are new to Android development then we should learn what an Activity is in Android and what is the lifecycle of an Activity.

A famous quote about learning is :

“Change is the end result of all true learning.”


So Let’s begin.


What is an Activity in Android?

The Activity class is a crucial component of an Android app, and the way activities are launched and put together is a fundamental part of the platform’s application model.

Whenever we open an Android application, then we see some UI drawn over our screen. That screen is called an Activity. It is the basic component of Android and whenever we are opening an application, then we are opening some activity.

For example, when we open our Gmail application, then we see our emails on the screen. Those emails are present in an Activity. If we open some particular email, then that email will be opened in some other Activity.

When we all started with coding, we know about the main method from where the program begins execution. Similarly, in Android, Activity is the one from where the Android Application starts its process. Activity is one screen of the app’s user interface. There is a series of methods that run in an activity.

There is a lifecycle associated with every Activity and to make an error-free Android application, we have to understand the lifecycle of Activity and write the code accordingly.


What is the Android Activity Lifecycle ?

As a user navigates through, out of, and back to our app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.

Within the lifecycle callback methods, we can declare how our activity behaves when the user leaves and re-enters the activity.

For example, if we’re building a streaming video player, we might pause the video and terminate the network connection when the user switches to another app. When the user returns, we can reconnect to the network and allow the user to resume the video from the same spot. In other words, each callback allows us to perform specific work that’s appropriate to a given change of state. Doing the right work at the right time and handling transitions properly make our app more robust and performant. For example, good implementation of the lifecycle callbacks can help ensure that our app avoids:

  • Crashing if the user receives a phone call or switches to another app while using our app.
  • Consuming valuable system resources when the user is not actively using it.
  • Losing the user’s progress if they leave our app and return to it at a later time.
  • Crashing or losing the user’s progress when the screen rotates between landscape and portrait orientation.

The Core Set Of Android Activity Lifecycle Callbacks

An Android activity undergoes through a number of states during its whole life cycle. To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate()onStart()onResume()onPause()onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state.

Source: Android Developer – Android Activity Lifecycle

 The Activity lifecycle consists of 7 methods:

  1. onCreate() : This method calls When a user first opens an activity. We must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the Created state. In the onCreate() method, we perform basic application startup logic that should happen only once for the entire life of the activity.
  2. onStart(): When the activity enters the Started state, the system invokes this callback. The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive..
  3. onResume(): When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. 
  4. onPause():  The system calls this method as the first indication that the user is leaving your activity. it indicates that the activity is no longer in the foreground. Use the onPause() method to pause or adjust operations that should not continue while the Activity is in the Paused state, and that we expect to resume shortly.
  5. onStop(): When our activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call onStop() when the activity has finished running, and is about to be terminated.
  6. onRestart(): It calls when the activity in the stopped state is about to start again.
  7. onDestroy(): It  calls when the activity clears from the application stack.The system invokes this callback either because:
    • the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
    • the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode).

So, these are the 7 methods that associates with the lifecycle of an activity.


Use-cases of Activity Lifecycle

Now, let’s see real-life use-cases to understand the lifecycle for an activity.


Use Case 01

When we open the activity for the first time, the sequence of state change it goes through is,

onCreate -> onStart -> onResume

After this point, The user uses the activity when it is ready.


Use Case 02

Now, let’s say we are minimizing the app by pressing the home button of the phone. The state changes it will go through is,

onPause -> onStop


Use Case 03

When we are moving to and from between activities, let’s say Activity A and Activity B. So, we will break it down into steps.

First, it opens Activity A, where the following states call initially,

onCreate -> onStart -> onResume

Then let’s say on a click of a button we opened Activity B. While opening Activity B, first, onPause will be called for Activity A and then,

onCreate -> onStart -> onResume

will call for Activity B. Then to finish this off, onStop of Activity A will be called and finally, Activity B would be loaded.

Now, when we press the back button from Activity B to Activity A, then first,onPause of Activity B is called and then,

onRestart -> onStart -> onResume

calls for Activity A and it displays to the user. Here we can see onRestart gets called rather then onCreate as it is restarting the activity and not creating it.

Then after onResume of Activity A is called then,

onStop -> onDestroy 

calls for Activity B and hence the activity destroys as the user has moved to Activity A.


Use Case 04

Pressing the lock button while activity is on then,

onPause -> onStop 

calls and when we reopen the app again,

onRestart -> onStart -> onResume

calls.


Use Case 05

When we kill the app from the recent app’s tray,

onPause -> onStop -> onDestroy 

it gets called. Here you can see we are getting the onDestroy state getting called as we are killing the instance of the activity.

When we now reopen the activity, it will call onCreate and not onRestart to start the activity.


Use Case 06

Consider a use-case where we need to ask permission from the user. Majority of the times we do it in onCreate.

Now, an edge case here is let’s say we navigate to a phone’s Settings app and deny the permission there, and then I came back to the initial app’s activity. Here, onCreate would not be called.

So, our permission check could not be satisfied here. To overcome this, onStart is the best place to put your permission check as it will handle the edge cases.

That’s all about in this article.


Conclusion

In this article, we learned about what an Activity is in Android and what is the lifecycle of an Activity . We also discussed the different Use-cases of Activity LifeCycle in Android.

Thanks for reading ! I hope you enjoyed and learned about Activity Lifecycle Concept in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolMonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Exit mobile version