Translate

Friday, 2 September 2016

What is Activity Life Cycle of Android ?

 Activity Life Cycle of Android 






OnCreate
This is the first method to be called when an activity is created. OnCreate is always overridden to perform any startup initializations that may be required by an Activity such as:

·       Creating views
·       Initializing variables
·       Binding static data to lists
OnCreate takes a Bundle parameter, which is a dictionary for storing and passing state information and objects between activities If the bundle is not null, this indicates the activity is restarting and it should restore its state from the previous instance. The following code illustrates how to retrieve values from the bundle:
protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);

   string intentString;
   bool intentBool;

   if (bundle != null)
   {
      intentString = bundle.GetString("myString");
      intentBool = bundle.GetBoolean("myBool");
   }

   // Set our view from the "main" layout resource
   SetContentView(Resource.Layout.Main);
}
Once OnCreate has finished, Android will call OnStart.
OnStart
This method is always called by the system after OnCreate is finished. Activities may 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. Android will call OnResume immediately after this method.
OnResume
The system calls this method when the Activity is ready to start interacting with the user. Activities should override this method to perform tasks such as:
·       Ramping up frame rates (a common task in game building)
·       Starting animations
·       Listening for GPS updates
·       Display any relevant alerts or dialogs
·       Wire up external event handlers
As an example, the following code snippet shows how to initialize the camera:
public void OnResume()
{
    base.OnResume(); // Always call the superclass first.

    if (_camera==null)
    {
        // Do camera initializations here
    }
}
OnResume is important because any operation that is done in OnPause should be un-done in OnResume, since it’s the only lifecycle method that is guaranteed to execute after OnPause when bringing the activity back to life.
OnPause
This method is called when the system is about to put the activity into the background or when the activity becomes partially obscured. Activities should override this method if they need to:
·       Commit unsaved changes to persistent data
·       Destroy or clean up other objects consuming resources
·       Ramp down frame rates and pausing animations
·       Unregister external event handlers or notification handlers (i.e. those that are tied to a service). This must be done to prevent Activity memory leaks.
·       Likewise, if the Activity has displayed any dialogs or alerts, they must be cleaned up with the .Dismiss() method.
As an example, the following code snippet will release the camera, as the Activity cannot make use of it while paused:
public void OnPause()
{
    base.OnPause(); // Always call the superclass first

    // Release the camera as other activities might need it
    if (_camera != null)
    {
        _camera.Release();
        _camera = null;
    }
}
There are two possible lifecycle methods that will be called after OnPause:
1.    OnResume will be called if the Activity is to be returned to the foreground.
2.    OnStop will be called if the Activity is being placed in the background.
OnStop
This method is called when the activity is no longer visible to the user. This happens when one of the following occurs:
·       A new activity is being started and is covering up this activity.
·       An existing activity is being brought to the foreground.
·       The activity is being destroyed.
OnStop may not always be called in low-memory situations, such as when Android is starved for resources and cannot properly background the Activity. For this reason, it is best not to rely on OnStop getting called when preparing an Activity for destruction. The next lifecycle methods that may be called after this one will be OnDestroy if the Activity is going away, or OnRestart if the Activity is coming back to interact with the user.
OnDestroy
This is the final method that is called on an Activity instance before it’s destroyed and completely removed from memory. In extreme situations Android may kill the application process that is hosting the Activity, which will result in OnDestroy not being invoked. Most Activities will not implement this method because most clean up and shut down has been done in theOnPause and OnStop methods. The OnDestroy method is typically overridden to clean up long running resources that might leak resources. An example of this might be background threads that were started in OnCreate.
There will be no lifecycle methods called after the Activity has been destroyed.
OnRestart
This method is called after your activity has been stopped, prior to it being started again. A good example of this would be when the user presses the home button while on an activity in the application. When this happens OnPause and then OnStopmethods are called, and the Activity is moved to the background but is not destroyed. If the user were then to restore the application by using the task manager or a similar application, Android will call the OnRestart method of the activity.

No comments:

Post a Comment