Android training-pause and resume activity

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/basics/activity-lifecycle/pausing.html#Pause

During application use, the foreground activity sometimes occupies the display position of other visual components, leading to the suspension of the blocked activity. For example, when a translucent activity is opened (such as a dialog box), the previous activity is paused. Although the activity is still partially visible, the current focus is not in the activity, so it is still paused (paused status ).

However, once the activity is completely blocked and invisible, it will be terminated ).

When your activity enters the paused state, the system will call the onpause () method of the activity. In the implementation of this method, you can terminate the pause operation or retain some information that should be permanently maintained. If a user returns to your activity from the paused status, the system restores the activity and calls the onresume () method.

Note: When your activity receives a call to the onpause () method, this indicates that the activity will be paused for a while and the user may return this activity. However, it usually indicates that the user is preparing to leave the activity.

Figure 1. When a translucent activity blocks your activity, the system will call the onpause () method of the activity that is blocked, and the activity will wait in paused state (1. If the user returns the paused activity, the system will call the onresume () (2) method.

Pause Activity

When the system calls the onpause () method of your activity, it technically means that your activity is still partially visible, but in most cases it means that the user is leaving the activity, and soon it will enter the stopped state. The onpause () callback method is usually used in the following cases:

1. Terminate the animation or cancel other CPU-consuming operations;

2. Submit unsaved changes, but only when the user leaves the activity and expects to persistently save such changes (such as the draft email)

3. Release System resources. If broadcast receivers, processing sensors (such as GPS), or any resources that may consume batteries when your activity is paused, the user no longer needs them.

For example, if your application uses camera, releasing it in the onpause () method is a good place.

@ 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 (mcamera! = NULL ){

Mcamera. Release ()

Mcamera = NULL;

}

}

Generally, you should not use the onpause () method to save user changes to be persisted (such as entering personal information in the form ). Only when you confirm that the user expects to automatically save the changes will the changes be stored persistently in the onpause () method. However, during the onpause () method, you should avoid CPU occupation, such as data writing, because this will slow down the display speed of the next activity (on the contrary, you should perform heavy load operations during the onstop () method ).

In fact, if your activity is terminated, you should try to perform relatively simple operations in your onpause () method so that users can quickly switch to the next target.

Note: When your activity is paused, the instance of the activity will be retained in the memory and called again when the activity is restored. In this way, the activity will be returned to the resumed status, and you do not need to reinitialize those components that have been created.

Restart Activity

When your activity returns the resumed status from the paused status, the system will call the onresume () method.

Note that every time the system calls this method, your activity will enter the foreground, including the first time it was created. For example, you should implement the onresume () method to initialize the released components that are released during the onpause () method, and the initialization operations that occur each time you enter the resumed State (such as starting an animation and initializing a component only when the activity obtains the user focus ).

The following onresume () method corresponds to the onpause () example above, so it initializes the camera object released when the activity is paused.

@Override
publicvoid 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.