Android App Development: Activity Life Cycle

The activity is the core of an android application. Each application can have one or more activities.

In this post we are going to explore the activity’s life cycle and understand the event handler of each stage through the activity’s life cycle.

Activities in the system are managed in an activity stack (Last in Last out). When a new activity is launched it becomes on the top of the stack. Any previous activity will be below it and won’t come to the top until the new one exists.

The application on the top of the stack has the highest priority from the operating system. While the activity that is not visible has lower priority even if the a running activity requires more memory, the system can shut down that activity to free memory.

Android runs each activity in a separate process each of which hosts a separate virtual machine. Android saves metadata (state) of each activity so that when a new activity launches so that it can come back to the activity when the user backtracks.

The activity can be in one of four states:

Active: the activity started, is running and is in the foreground.

Paused: the activity is running and visible but another activity (non full sized) is running on the top or a notification is displayed. The user can see the activity but can not interact with it. A paused activity is fully alive (maintains state and member information) but can be killed by the system in low memory situations.

Stopped: the activity is running but invisible because the user has launched another activity that comes to the foreground the activity is alive (maintains state and member information) but can be killed by the system in low memory situations.

Dead: either the activity is not started or it was in pause or stop state and was terminated by the system to free some memory or by asking the user to do so.

The following figure shows the states of the activity and the methods that handle each state

The sequence is as follows:

  • The activity starts, passes through onCreate(), onStart() the activity is still not visible to the user, onResume() then it comes to the foreground and becomes fully running.
  • If another activity launches or a notification appears the activity passes through the onPause() method. Then there would be two scenarios:
    1.if the system decides to kill your activity due to low memory the activity starts the cycle again from onCreate() method with Bundle savedInstanceState parameter that holds data about the previous state of the activity.
    2.If the user resumes the activity by closing the new activity or the notification the activity cycle continues from the onResume() method
  • When the user is about to close the activity the activity calls onStop() method then onDestroy() method when the system destroys the activity.
    But if another activity runs while the current one is was not shut, the activity calles onStop() method and if it is not killed by the system it will call onRestart() method then onStart() mehod and continues the cycle.
  • onCreate(): will be invoked in three cases:
    - the activity runs for the first time and it will be invoked with null Bundle savedInstanceState parameter.
    - the activity has been running then stopped by the user or destroyed by the system then it would be invoked with Bundle savedInstanceState that holds the previous state of the activity.
    - the activity is running and you set the device to different resources like Portrait vs landscape, then the activity will be recreated.in this method you will create the user interface, bind data to controls and register the event handlers for the controls. Then it is followed by onStart() method.
  • onStart(): will be invoked when the activity is first launched or brought back to the foreground
    it would be followed by onResume() if the activity continues and comes to foreground, or by onStop() if the activity is killed.
  • onRestart(): is invoked in case the activity has been stopped and is about to be run again. Always followed by onStart() mehod.
  • onResume(); invoked when the activity is about to come to the foreground and is on the top of the activity stack. It is the place where you can refresh the controls if the activity is using a service that displays some feeds or news. Always followed by onPause() method.
  • onPause(): is invoked when another activity launches while the current activity is launched or when the system decides to kill the activity. In this method you have to cancel everything you did in onResume() method like Stopping threads, stopping animations or clearing usage of resources(eg the camera).This method is followed by onResume() if the activity returns back to front or by onStop() if the activity is to be invisible.
  • onStop(): is invoked when a new activity is about to come over the current one or the current one is to be destroyed. Always followed by onResume() if the activity comes back or onDestroy() if the activity is to be killed.
  • onDestroy():is invoked when the activity is shutting down because the activity called finish() [terminated the activity] or because the system needs memory so it decided to kill the activity.

Killable methods:

There are methods that are “killable” meaning that after theses methods return, the process hosting them can kill the activity without executing any further code (due to lack of memory)

These methods are onPause(), onStop() and onDestroy()

here’s the declaration of the life cycle methods:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
    }

    @Override
    protected void onPause() {
    	super.onPause();
    }
    @Override
    protected void onResume() {
    	super.onResume();
    }
    @Override
    protected void onRestart() {
    	super.onRestart();
    }
    @Override
    protected void onStop() {
    	super.onStop();
    }
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    }

Summary:

  • The entire activity life cycle is between the onCreate() where you construct the UI and aquire resources and onDestroy() method where you release all resources.
  • The visible life time of the activity is between onStart() and onStop(). Between the activity is visible to the user although he may be unable to interact with it. Between the two methods you persist the state of the activity so that if another one comes to the foreground then comes back to the original activity you find the state persisted.
  • The foreground lifetime is between the onResume() and onPause(). During this time the activity is fully interactive with the user. The activity can go through the resume and pause states many times (if the device sleeps or a new activity launches) .

7 Responses to “Android App Development: Activity Life Cycle”

  1. David Clements 26. Jan, 2011 at 10:47 pm #

    Are you sure that activities each run in their own process? In my apps I use a central singleton class with static variables to share data across activities. Android would have to jump through some hoops to translate this to shared memory and allow access from different processes.

    I think they are in different thread within the same VM.

  2. Eric Berry 31. Jan, 2011 at 10:14 am #

    Thanks for this article. It is extremely helpful!

  3. Ira Attleson 21. Aug, 2011 at 7:37 am #

    There is many myths ongoing about app development and average people like you and I can win if we are fit to put away our concerns and curl up our sleeves and get our feet and legs wet. Every bit as tough or worst is not having any idea as to where to start or the schemes to use.

  4. vinod kalligudd 20. Jan, 2012 at 5:45 am #

    Great concepts, helped me a lot….

  5. Arena Social 12. Mar, 2012 at 7:31 am #

    I don’t understand this coding but surely say this is a very useful post.

  6. jake 14. Mar, 2013 at 5:49 am #

    very nice concepts. it helps me a lot.

Trackbacks/Pingbacks

  1. Tweets that mention Android App Development: Activity Life Cycle -- Topsy.com - 26. Jan, 2011

    [...] This post was mentioned on Twitter by Mobile Orchard Team, ryoibot. ryoibot said: [MobileOrchard] Android App Development: Activity Life Cycle: The activity is the core of an android app… http://bit.ly/hjDdaa [...]

Leave a Reply

© 2008-2051 • Mobile Orchard and the Bar-Tree Logo are service marks. ContactAdvertise