Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Android Activity LifeCycle

Activity is the base class of an Android Activity (package An Activity represents a view to the user. For eg. When we open Messaging app to write a new message, it is an Activity. After writing the message, when we open contacts to select the person, it can be considered as another activity and so on. So in simpler words, anything which completely occupies the screen and is different from the previous screen is called an Activity. In this tutorial we'll get to know about Android Activity Lifecycle.


Two screens showing different activities

As shown in previous tutorials, each android activity extends the class. Activity is a concrete class and contains many methods which can be overwritten so that the activity behaves as per our choice. Such methods are called as lifecycle methods (discussed below).

Note : Each new activity must extend the Activity class (or its direct subclasses) and have corresponding entry in the AndroidManifest.xml file as shown below


There are many methods to go from one activity to another. Also, we can either choose to finish the previous activity, or leave it as it is.

LifeCycle of Android Activity


Source : Android Developers Website

Some of you may have seen this screenshot in various books or on websites. Lifecycle is a fundamental concept in android which consists of many methods from Activity class. Any practical application uses at least one lifecycle method. onCreate() is one such method. People knowing about Java Applets must be knowing what a lifecycle methods are, for others you’ll get to know shortly.

The image shown above gives an overview of basic lifecycle methods. When an application starts, few of them are called. When it exits, different set of methods are called. Similarly when you navigate from one activity to another or come back, some methods are called. Below we have summarized what all methods are called in which situations.

(The parameters, return types and access specifiers are not shown for now. )

Below I have summarized what all methods are called in which situations.

1. When an application starts, these methods are called :

a)     onCreate()

b)    onStart()

c)     onResume().

2. When some other application is called, while your application was running :

a)     onPause()

b)    onStop() are called as new activity completely blocks the screen.

3. When you go back to your application, which was running previously,

a)     onRestart()

b)    onStart()

c)     onResume() are called. Here,it is similar to 1st one except for the difference that onRestart() is called rather than onCreate(). This is because the app exists in memory and just need to be loaded from it.

4. When the user presses back button to exit the application or there is no memory for the app, the onDestroy() method is called. In normal case, the order of execution for a running application would be,

a)     onPause()

b)    onStop()

c)     onDestroy().

Difference between “onPause()”   and   “onPause() + onStop()” 

There are some situations when some new activity partially occupies the screen. In such cases, some part of our activity is still visible to the user and is not shifted to BackStackOn the other hand, there are some cases when some new activity or application completely blocks the GUI (or screen), our activity’s GUI is shifted to BackStack. So in the first case (partially occupied), only the onPause() method is called. Whereas in the second case (completely blocked GUI), onPause() + onStop() methods are called. I hope you have now understood the difference between the situations where one or both of them are called.

In the next tutorial, I’ll show the code for the same thing which we have discussed till now. It will also cover debugging in android using a special class (android.util.Log). Stay tuned for more tutorials.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy