7) 10 minutes to learn the pause and recovery of the life cycle of android--activity

Source: Internet
Author: User

When the app is used normally, the activity on the front end is sometimes blocked by other visible components (obstructed), which causes the current activity to enter the pause state. For example, when you open a translucent activity (for example, in the form of a dialog box), the previous activity is paused. As long as the previous activity is still partially visible, the activity will remain in the paused state.

However, once the previous activity is completely blocked and not visible, it goes into the stop state (discussed in the next section).

Once the activity enters the paused state, the system invokes the OnPause () method in activity, which can stop actions that should not be performed during the pause, such as pausing video playback, or saving information that might require long-term preservation. If the user returns to the current activity from the paused state, the system should restore the data and execute the Onresume () method.

Note: When our activity receives a signal to call OnPause (), that may mean that the activity will be suspended for a period of time, and the user is likely to return to our activity. However, that is also the first signal that users want to leave our activtiy.

Figure 1. When a translucent activity blocks activity, the system calls the OnPause () method and the activity stays in the paused state (1). If the user returns to the activity while the activity is still in the paused state, the system calls its Onresume () (2).

Suspend activity

When the system invokes OnPause () in activity, technically, it means that the activity is still partially visible. But more often it means that the user is leaving the activity and will immediately enter stopped state. You should usually do the following in the OnPause () callback method:

    • Stopping animations or other running operations can lead to a waste of CPU.
    • Submit content that is expected to be saved when the user leaves (such as a draft of a message).
    • Frees system resources, such as broadcast receivers, sensors (such as GPS), or any other resource that can affect power.

For example, if a program uses Camera,onpause () it is a good place to do things that release resources.

@Override  Public void OnPause () {    Super. OnPause ();  // Always call the superclass method     First // Release The Camera because we don ' t need it when paused     // and other activities might need to use it.    if NULL {        mcamera.release        ()null;    }}

In general, OnPause () should not be used to save user-changed data (for example, by filling in a table of personal information) to permanent storage (file or DB). Only when you are sure that users expect those changes to be saved automatically (for example, by writing a draft of a message), store those data in permanent storage. However, we should avoid doing cpu-intensive work at OnPause (), such as writing data to DB, because it will cause the switch to the next activity to become slow (should put those heavy-load's work into OnStop ().

If the activity is actually to be stop, then we should reduce the amount of work in the OnPause () method in order to switch smoothly.

Note: When activity is paused, the activity instance resides in memory and is re-invoked when the activity resumes. We do not need to reinitialize the component in a series of callback methods that revert to the resumed state.

Resume Activity

When the user resumes activity from the paused state, the system calls the Onresume () method.

Note that every time the system calls this method, the activity is in the foreground, including the first time it is created. Therefore, you should implement Onresume () to initialize those components that are released within the OnPause method and perform the initialization actions that the activity requires each time it enters the resumed state (such as starting animations and initializing components that are only needed when the user focus is acquired)

The following example of Onresume () corresponds to the above example of the OnPause ().

@Override  Public void Onresume () {    Super. Onresume ();  // Always call the superclass method     First // Get The Camera instance as the activity achieves full user focus    if NULL ) {        //  Local method to handle camera init    }}

 

7) 10 minutes to learn the pause and recovery of the life cycle of android--activity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.