MY mENU


Monday 13 February 2012

Activity and it's Lifecycle

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. An activity presents a visual user interface for one focused endeavor the user can undertake. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. 


When an activity is stopped because a new activity starts, it is notified of this change in state through the activity's life-cycle callback methods. There are several callback methods that an activity might receive, due to a change in its stateThese state transitions are all part of the activity life-cycle.
1. onCreate();  must implemented method. The system calls this when creating your activity. Within your implementation, you should initialize the essential components of your activity. Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface.
2. onStart(); Called just before the activity becomes visible to the user.
3. onResume(); The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)

4. onPause(); calls this method as the first indication that the user is leaving your activity (not destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

5. onStop(); Called when the activity is no longer visible to the user, may happen because it is being destroyed.

6. onDestroy(); Called before the activity is destroyed. It could be called while the activity is finishing.

onSaveInstanceState() - called if the activity is stopped. Used to save data of that activity so that the activity can restore its states if it re-started.
public class ExAct extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
}

through implementing these life cycle we described as below
Entire Lifetime of an Activity comes between onCreate() to onDestroy().
Visible Lifetime of an Activity comes between onStart() to onStop().
Foreground Lifetime of an Activity comes between onResume() to onPause(). 



No comments:

Post a Comment