Wednesday, February 15, 2012

Activity Life Cycle

Activity :

An Activity is one of the most important component of android applications because it's related to the user interface.An Activity Provide an window in which we draw interface(like windows forms,web pages ) for user by which user can interact which our application.

Activity Life Cycle States:

  • Entire Lifetime:
    Entire Life Cycle of an Activity happens when system calls onCreate() and onDestroy() methods of our Activty.
  • Visible Lifetime:
    Visible Life Cycle of an Android Activity happens when system calls onStart() and onStop() methods of our Activty.
  • Foreground Lifetime:
    Foreground Life Cycle of an Activity happens when system calls onResume() and onPouse() methods of our Activty.

Android_Activity_Life_cycle



Activity Callback Methods:

The Activity class has a number of callbacks methods that system will calls when an Activity pass from Life Cycle States.Most important callback methods:

  • onContentChanged():

    Called whenever the content view of the screen changes or this method must be called by System prior to OnCreate().

  • onCreate():

    This method is called by system when Activity is first created.this is the place where we can setup all UI elements,Layouts for an Activity.

  • onStart():

    This method is called when the activity is about to become visible to the user. Activities should 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, or ramping up frame rates (a common task in game building).

  • onRestoreInstanceState(Bundle restoreInstanceState):

    This method is called by system for allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(). This function will never be called with a null state.

  • onPostCreate(Bundle bundle):

    Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called). Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.

  • onResume():

    This method is called when the activity will start interacting with the user after being in apause state. When this method is called, the activity is moving to the top of the activity stack, and it is receiving user input. Activities can override this method if they need to perform any tasks after the activity begins accepting user input.

  • onPostResume ():

    Called when activity resume is complete (after onResume() has been called). Applications will generally not implement this method; it is intended for system classes to do final setup after application resume code has run.

  • onAttachedToWindow():

    Called when the main window associated with the activity has been attached to the window manager.

  • onUserInteraction():

    Called whenever a key, touch,back key or trackball event is dispatched to the activity.

  • onUserLeaveHint():

    Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted.

  • onPause():

    This method is called when the system is about to put the activity into the background.Activities must override this method if they need to commit unsaved changes to persistent data, destroy or cleanup other objects consuming resources, or ramp down frame rates,etc. Implementations of this method should return as quickly as possible, as no successive activity will be resumed until this method returns.

  • onSaveInstanceState(Bundle savedInstanceState):

    This method is provided by the Android activity lifecycle framework to give an activity the opportunity to save its data when a change occurs, for example, a screen orientation change.

  • onStop():

    This method is called when the activity is no longer visible to the user, because another activity has been resumed or started and is covering this one. This can happen because the activity is completing (Finish method was called) because the system is destroying this instance of the activity to save resources, or because an orientation change has occurred for the device.

  • onDestroy():

    This is the final method that is called on an activity before it’s destroyed. After this method is called, your activity will be killed and purged from the resource pools of the device. The OS will permanently destroy the state data of an activity after this method runs, so an activity should override this method as a final means to save state data.

  • onDetachedFromWindow():

    Called when the main window associated with the activity has been detached from the window manager.

  • onRestart():

    This method is called after your activity has been stopped, prior to it being started again.This method is always followed by OnStart. An activity should override OnRestart if it needs to perform any tasks immediately before OnStart is called. For instance, if the activity has previously been sent to the background and OnStop has been called, but the activity’s process has not yet been destroyed by the OS, then the OnRestart method should be overridden.










 
Powered by Blogger