Sunday, January 17, 2010

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()

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 on Pause(). 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) so the code should be very lightweight.

No comments:

Post a Comment