Android activity OnPause () and Onresume ()

Source: Internet
Author: User

Official Document Address: Http://www.android-doc.com/training/basics/activity-lifecycle/pausing.html#Resume


Pause and resume an activity

In general app usage, the foreground activity is usually obscured by the visual component, which causes the activity to pause.

For example, when a translucent activity is opened (just like some form of dialog box). The activity before this will pause. Only the activity is still partially visible, but the current activity is not operational and it is in pause.

But. Once the activity has been obscured, it is not visible. It's stop now.

(This will be discussed in the next lesson)

When your activity enters the pause state, the system invokes the OnPause () method in the activity, OnPause () This method allows you to end some of the tasks that are in progress, which cannot be continued at the time of the stop (for example, a video). It also allows you to leave your app when the user insists on leaving. Retain information that should be kept permanently. Assume that the user has returned to your activity from the state of pause. This system resume this activity and calls the Onresume () method.


Note: When your activity receives a request to call OnPause (), it may indicate that the activity will be stopped for a period of time and that the user is likely to return to your activity again.

But it's also very likely that users are leaving your app.


Picture: When a translucent activity obscures your activity, the system calls OnPause () and the activity waits in the state of pause.

Suppose the user returns to activity again when it still has pause. This system will call Onresume ().

Pause your activity.

When the system calls OnPause () for your activity. Reasonable words. It means that your activity is still partially visible, but it is also very likely that the user is leaving your activity and that it will soon enter the stop state.

You should often use OnPause () in the following situations.

1. End CPU-consuming animations or other ongoing tasks.

2, the submission of no saved changes. But only when the user leaves the hope that these changes are saved.

3, release the system resources, just like the broadcast receiver. Manipulate the sensor (like a GPS), or any resource that might affect your battery life, after acitivity is pause and the user doesn't need it.

For example, if your application uses a camera, then the OnPause () method is a very good way to release it.

@Overridepublic 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 (Mcamera! = null) {        mcamera.release ()        mcamera = null;}    }

On the whole, you should not use OnPause () to save the user's changes to a permanent storage space (for example, form input personal information).

The only case where you need to use OnPause () to store the user's changes in permanent memory is. When one of your county users needs to be proactive in their own preservation (for example, write an email). However, in OnPause () you should avoid running CPU-intensive work, such as writing to a database. As this slows down the speed at which you can visualize the transition to the next activity.

(You should put this heavy closing operation in OnStop ())

In the OnPause () method, you should maintain a portion of the operation. To ensure that your activity ends up jumping to the next user destination at a faster speed.

Note: When your activity is pause, the activity instance is occupied in memory and will be awakened again when the activity is resume. You do not need to initialize any of the components that have already been created, regardless of the callback function, before the resume state.

Resume your activity.

When the user resume your activity from the state of pause, the system calls the function of Onresume ().

Consider that this function is called every time the system is running into the foreground. Even when the activity has been created. The same you should implement Onresume () to initialize the components that you release in OnPause (). And also run some other initialization that needs to run when the activity enters the resume state (for example, to open the animation, initialize only the components that the activity has to use when it gets the focus).

The following example of Onresume () is matched with OnPause () in the sample above. So it initializes the camera that needs to be released when the activity is pause.

@Overridepublic void Onresume () {    super.onresume ();  Always call the superclass method first    //Get The Camera instance as the activity achieves full user focus    if (Mcamera = = null) {        Initializecamera ();//Local method to handle camera init    }}


Android activity OnPause () and Onresume ()

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.