Pause and resume Activity Android

Source: Internet
Author: User

Suspend and resume activity (pausing and Resuming an activity)

In a normal application, the foreground activity is sometimes obscured by other visual components, which can cause activity pauses. For example, when a semi-transparent activity is opened (as in a style dialog), the previous activity is paused. As long as the activity is still partially visible, but it is not currently in focus, it is still in a paused state.

However, once the activity is completely obscured and invisible to the user, it stops (this is what the next lesson needs to discuss).

When your activity goes into a paused state, the system will call the OnPause () method on your activity. In this method, you can stop any behavior that should not continue in the paused state (such as video playback), or persist some information that needs to be persisted so that the user continues to stay in your application. If the user returns to your activity, the system resumes it from the paused state and calls the Onresume () method.

Note: When your activity receives a call to OnPause (), it may be a sign that the activity will be paused for a while and then the user will return to your activity. However, it is usually the first sign that the user is leaving your activity.

Figure 1. When a translucent activity obscures your activity, the system calls OnPause (), activity, and so on in a paused state (1). If the user returns to an activity that is still in a paused state, Onresume () (2) is called.

Pause your activity (pause Your activity)

When the system calls OnPause () for your activity, technically, your activity is still partially visible, but the most common indication is that the user is leaving the activity, and it will soon enter the Stop (Stopped) state. You should usually use the OnPause () callback:
Stops the animation effect or other behavior that is consuming CPU resources in progress.

Commit unsaved changes, but only if the user wants to: when they leave, they need to make a permanent save of the changes (such as composing an email).
Releasing some system resources, such as a broadcast receiver, processing a sensor (such as a GPS), or any resource that affects battery life, while your activity is suspended, users no longer need these resources.

For example, if your application uses a camera, then the OnPause () method is a good place 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;}    }

In general, you should not use OnPause () to save user changes (such as personal information input) to persistent storage. You can write the user's changes in the OnPause () method to the persisted store only if you are sure that the user expects to automatically save their changes (such as drafting an email). However, you should avoid performing high-CPU workloads in OnPause (), such as reading and writing databases, as it slows down the transition to the next activity (you should perform a heavy-load shutdown action in OnStop ()).

You should keep the volume of business in the OnPause () method relatively simple, in order to allow a quick transition to the user's next destination if your activity is actually stopped.

Note: When your activity is paused, the activity instance also resides in memory and is re-invoked when the activity resumes. When you call any callback method to revert to the recovery state, you do not need to reinitialize those components that have already been created.

Resume your activity (resume Your activity)

The Onresume () method is called when the user resumes your activity from a paused state.

Note that the system calls this method every time your activity enters the foreground, including when it was first created. Therefore, you should initialize those components that you will release in OnPause () when you implement Onresume (), and perform those that each activity The initialization action that must be completed when you enter the recovery state (such as the component that you want to use when you start the animation and initialize the activity to get the user focus).

The following example Onresume () is the corresponding OnPause () example above, so it initializes the camera that was released when the activity was paused.

@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    }}
Related Article

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.