Lifecycle of an Android Activity

by Mir Suhail

In android applications, activities are a fundamental building block and they can exist in a number of different states. The lifecycle of an android activity begins with its instantiation and ends with destruction, and it includes many states in between. When an activity changes state, the appropriate lifecycle event method is called, notifying the activity of the impending state change and allowing it to execute code in order to adapt to that change.

Activity States

The Android OS uses a priority queue to assist in managing activities running on the device. Based on the state, a particular Android activity is in, it will be assigned a certain priority within the OS. This priority system helps Android identify activities that are no longer in use, allowing the OS to reclaim memory and resources. The following diagram illustrates the states an activity can go through, during its lifetime:

states
Image courtesy: https://developer.xamarin.com/guides/android/application_fundamentals/activity_lifecycle/

 

Activity Lifecycle Methods

As mentioned above, when an activity state is changed, the appropriate lifecycle method is called to impend the state changes and to allow it to execute the code in order to adopt that state change. The various callback methods associated with an activity life cycle and their relation with the activity states are shown below:

activity_lifecycle
Image courtesy: http://www.slideshare.net/basantdewangan/mobile-testing-android

 

 

Callback Description
onCreate() This is the first callback and called when the activity is first created. OnCreate is always overridden to perform any startup initializations that may be required by an Activity such as: creating views, Initializing variables etc.
onStart() This callback is called when the activity becomes visible to user after OnCreate is finished. Activities may override this method if they need to perform any specific tasks right before an activity becomes visible such as refreshing current values of views within the activity. Android will call OnResume immediately after this method.
onResume() The system calls this method when Activity is ready to start interacting with the user. Activities should override this method to perform tasks such as: Starting animations, Listening for GPS updates, Display any relevant alerts or dialogs etc.
onPause() The paused activity does not receive user input and cannot execute any code and is called when current activity is being paused and previous activity is being resumed.
onStop() This callback is called when activity is no longer visible. The next lifecycle method that may be called after this one will be OnDestroy if the Activity is going away, or OnRestart if the Activity is coming back to interact with user.
onDestroy() This callback is called before the activity is destroyed by system. The OnDestroy method is typically overridden to clean up long running resources that might leak resources. An example of this might be background threads that were started in OnCreate.
onRestart() This callback is called when activity restarts after stopping it. A good example of this would be when the user presses the home button while on an activity in the application. When this happens OnPause and then OnStop methods are called, and Activity is moved to the background but is not destroyed. If the user were then to restore application by using task manager or a similar application, Android will call OnRestart method of the activity.

The following example illustrates the various lifecycle methods:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
    String tag = "LifeCycleEvents";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       Log.d(tag, "In the onCreate() event");
    }
    public void onStart()
    {
       super.onStart();
       Log.d(tag, "In the onStart() event");
    }
    public void onRestart()
    {
       super.onRestart();
       Log.d(tag, "In the onRestart() event");
    }
    public void onResume()
    {
       super.onResume();
       Log.d(tag, "In the onResume() event");
    }
    public void onPause()
    {
       super.onPause();
       Log.d(tag, "In the onPause() event");
    }
    public void onStop()
    {
       super.onStop();
       Log.d(tag, "In the onStop() event");
    }
    public void onDestroy()
    {
       super.onDestroy();
       Log.d(tag, "In the onDestroy() event");
    }
}

 

One thought on “Lifecycle of an Android Activity”

  1. It was very useful using your log commands to see when exactly the lifecycle methods are being used. Something interesting you can try to having other activities in cover your current activity, or having them visible, but not in the foreground. Or perhaps being visible but not in the foreground, and then rotating the device.

    There are a lot of different combinations to consider when analyzing these different life cycle methods. I found it fun to try to predict what methods would be called when I was going to perform a certain task.

    For example, which methods would be called if a user starts the activity, starts using it, then switches to another app?
    The answer would be:
    onCreate -> onStart -> onResume-> onPause -> onStop

    See if you can figure this one out!
    User starts activity, starts using it, rotates the device, switched to another app, then goes back to that activity.
    This one is tough!
    If you think you got the answer feel free to send me an email to
    erick@androidsyndicate.com
    Also if your interested an example Stopwatch project that goes over activity life cycles, visit my blog at http://www.androidsyndicate.com

    Happy coding everyone! 🙂
    Erick Shaffer

Leave a Reply

Your email address will not be published. Required fields are marked *

Mobile Research Apps

We have deep expertise in design and development of mobile research apps that work on both iOS and Android securely.

Contact us now

Popular Posts