您的位置:首页 > 其它

Activity生命周期官方原文文档

2016-08-24 16:18 309 查看
Activity Lifecycle

The Android activity lifecycle comprises a collection of methods exposed within the Activity class that provide the developer with a resource management framework. This framework allows developers to meet the unique state management requirements of each activity within an application and properly handle resource management.

Activity States

The Android OS arbitrates Activities based on their state. This helps Android identify activities that are no longer in use, allowing the OS to reclaim memory and resources. The following diagram illustrates the states an Activity can go through during its lifetime:

These states can be broken into 4 main groups as follows:

Active or Running - Activities are considered active or running if they are in the foreground, also known as the top of the activity stack. This is considered the highest priority activity in Android, and as such will only be killed by the OS in extreme situations, such as if the activity tries to use more memory than is available on the device as this could cause the UI to become unresponsive.

Paused - When the device goes to sleep, or an activity is still visible but partially hidden by a new, non-full-sized or transparent activity, the activity is considered paused. Paused activities are still alive, that is, they maintain all state and member information, and remain attached to the window manager. This is considered to be the second highest priority activity in Android and, as such, will only be killed by the OS if killing this activity will satisfy the resource requirements needed to keep the Active/Running Activity stable and responsive.

Stopped/Backgrounded - Activities that are completely obscured by another activity are considered stopped or in the background. Stopped activities still try to retain their state and member information for as long as possible, but stopped activities are considered to be the lowest priority of the three states and, as such, the OS will kill activities in this state first to satisfy the resource requirements of higher priority activities.

Restarted – It is possible for an activity that is anywhere from paused to stopped in the lifecycle to be removed from memory by Android. If the user navigates back to the activity it must be restarted, restored to its previously saved state, and then displayed to the user.

Activity Lifecycle Methods

The Android SDK and, by extension, the Xamarin.Android framework provide a powerful model for managing the state of activities within an application. When an activity’s state is changing, the activity is notified by the OS, which calls specific methods on that activity. The following diagram illustrates these methods in relationship to the Activity Lifecycle:

As a developer, you can handle state changes by overriding these methods within an activity. It’s important to note, however, that all lifecycle methods are called on the UI thread and will block the OS from performing the next piece of UI work, such as hiding the current activity, displaying a new activity, etc. As such, code in these methods should be as brief as possible to make an application feel well performing. Any long-running tasks should be executed on a background thread.

Let’s examine each of these lifecycle methods and their use:

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:

OnResume will be called if the Activity is to be returned to the foreground.

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 the OnPause 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 OnStop methods 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.

There are no general guidelines for what kind of logic should be implemented in OnRestart. This is because OnStart is always invoked regardless of whether the Activity is being created or being restarted, so any resources required by the Activity should be initialized in OnStart, rather than OnRestart.

The next lifecycle method called after OnRestart will be OnStart.

Back vs. Home

Many Android devices have two distinct buttons: a “Back” button and a “Home” button. An example of this can be seen in the following screenshot of Android 4.0.3:

There is a subtle difference between the two buttons, even though they appear to have the same effect of putting an application in the background. When a user clicks the Back button, they are telling Android that they are done with the activity. Android will destroy the Activity. In contrast, when the user clicks the Home button the activity is merely placed into the background – Android will not kill the activity.

Managing State Throughout the Lifecycle

When an Activity is stopped or destroyed the system provides an opportunity to save the state of the Activity for later rehydration. This saved state is referred to as instance state. Android provides three options for storing instance state during the Activity lifecycle:

Storing primitive values in a Dictionary known as a Bundle that Android will use to save state.

Creating a custom class that will hold complex values such as bitmaps. Android will use this custom class to save state.

Circumventing the configuration change lifecycle and assuming complete responsibility for maintaining state in the activity.

This guide covers the first two options.

Bundle State

The primary option for saving instance state is to use a key/value dictionary object known as a bundle. Recall that when an Activity is created that the OnCreate method is passed a bundle as a parameter, this bundle can be used to restore the instance state. It is not recommended to use a bundle for more complex data that won’t quickly or easily serialize to key/value pairs (such as bitmaps); rather, it should be used for simple values like strings.

An Activity provides methods to help with saving and retrieving the instance state in the Bundle:

OnSaveInstanceState – This is invoked by Android when the activity is being destroyed. Activities can implement this method if they need to persist any key/value state items.

OnRestoreInstanceState – This is called after the OnCreate method is finished, and provides another opportunity for an Activity to restore its state after initialization is complete.

The following diagram illustrates how these methods are used:

OnSaveInstanceState

This method will be called as the Activity is being stopped. It will receive a bundle parameter that the Activity can store its state in. When a device experiences a configuration change, an Activity can use the Bundle object that is passed in to preserve the Activity state by overriding OnSaveInstanceState. For example, consider the following code:

int c;

protected override void OnCreate (Bundle bundle)

{

base.OnCreate (bundle);

this.SetContentView (Resource.Layout.SimpleStateView);

var output = this.FindViewById (Resource.Id.outputText);

if (bundle != null) {

c = bundle.GetInt (“counter”, -1);

} else {

c = -1;

}

output.Text = c.ToString ();

var incrementCounter = this.FindViewById (Resource.Id.incrementCounter);

incrementCounter.Click += (s,e) => {

output.Text = (++c).ToString();

};

}

The code above increments an integer named c when a button named incrementCounter is clicked, displaying the result in a TextView named output. When a configuration change happens - for example, when the device is rotated - the above code would lose the value of c because the bundle would be null, as shown in the figure below:

In order to preserve the value of c in this example, the Activity can override OnSaveInstanceState, saving the value in the bundle as shown below:

protected override void OnSaveInstanceState (Bundle outState)

{

outState.PutInt (“counter”, c);

base.OnSaveInstanceState (outState);

}

Now when the device is rotated to a new orientation, the integer is saved in the bundle and is retrieved with the line:

c = bundle.GetInt (“counter”, -1);

Note: It is important to always call the base implementation of OnSaveInstanceState so that the state of the view hierarchy can also be saved.

View State

Overriding OnSaveInstanceState is an appropriate mechanism for saving transient data in an Activity across orientation changes, such as the counter in the above example. However, the default implementation of OnSaveInstanceState will take care of saving transient data in the UI for every view, so long as each view has an ID assigned. For example, say an application has an EditText element defined in XML as follows:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐