Friday 4 May 2012

Android Activity Lifecycle :Understanding it completely with a small example.

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.

Managing the lifecycle of your activities by implementing callback methods is crucial to developing a strong and flexible application. The lifecycle of an activity is directly affected by its association with other activities, its task and back stack.
  • Activity contains the following states in its entire life-cycle.



  1. onCreate();
  2. onStrart();
  3. onResume();
  4. onPause();
  5. onStop();
  6. onDestroy();
  7. onRestrat();

An activity can exist in essentially three states:
Resumed
The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)
Paused
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (theActivity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish()method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.
To make an effective and stable application you should have a clear idea about the Activity Life-cycle.As many of the developers doesn't consider all the states of an activity .To get an idea i have created a simple application that contains three Activity, we can navigate from one activity to another .
  • Download the source code and try to run it in your Eclipse .
  • You can download the code from this link.
  • After starting the application you will see this screens Image :1,Image :2. with Buttons to navigate between Activities. 
  •     
    Image:1
    Image :2
  • Take a look at the LogCat file it will give you a clear vision and proper understanding of an Activity Life-cycle .
  • The logcat will show you something like the Image :3

After navigating from one activity to other try something different , press the Home key button,then again come back to the application.See the changes in the log . I think it will clear all the confusion in your mind about Activity Life-cycle .