[Android training video series] 2.2 pausing and resuming an activity

Source: Internet
Author: User

1. Main Content
This topic describes onpause and onresume, and mainly analyzes what operations should be performed in the onpause function.

2. Video description

Http://www.eyeandroid.com/thread-11291-1-1.html

3. Translation Reference

In normal program use, the activity running on the foreground is sometimes blocked by other visible components, causing the activity to be paused. For example, if a translucent activity (similar to a dialog box) is opened, the previous activity will be paused. Although this activity is partially visible, the current focus is not on this activity, so it remains in the paused state.

However, once this activity is completely blocked and in an invisible state, it is stopped. (Discussed in the next tutorial) If your activity is in the paused state, the system will call the onpause () method of your activity, this method allows you to pause behaviors that should not continue running (such as pausing video playback) or save user data that should be permanently saved. Once the user returns from the paused status, the system can call the onresume () method to restore the previous running. Tip: when the activity receives the onpause () call, this may indicate that the activity must temporarily enter the paused state, and the user may immediately obtain the focus of the activity. However, it is also the first method that will be called when the user leaves the activity. Illustration: When a translucent activity overwrites your activity, the system calls onpause () and the activity will wait (1) in the paused state ). The system calls onresume () to return activity. Pause your activity When the system calls the onpause () method, it technically indicates that the activity can still be partially visible, but in most cases it indicates that the user wants to leave the activity and it will enter the stopped state. You usually use the onpause () function to do the following:
  • Stop CPU-consuming animation or running action.
  • Submit unsaved changes, but only save the items that the user expects to save permanently after leaving (such as the draft email ).
  • Release system resources, such as broadcast receivers, sensors (such as GPS), or power-consuming resources that are no longer needed after the activity is suspended.
For example, if your program needs to use a camera, the onpause () method is a good place to release it.

 
  1. @ Override
  2. Public void onpause (){
  3. Super. onpause (); // The parent class method is usually called first.
  4. // Release camera Resources
  5. If (mcamera! = NULL ){
  6. Mcamera. Release ()
  7. Mcamera = NULL;
  8. }
  9. }

Generally, you do not need to use the onpause () method to store user changes (such as information filling forms) to fixed storage. The onpause () method must be used only when you really want to automatically save data (such as a draft email. You should try to avoid intensive CPU work in the onpause () method, such as writing data to the database, because it will slow you down into the next activity (you should stop in onstop () ). If your activity really needs to be stopped, you should keep the onpause () method as simple as possible to ensure quick conversion to the user's next destination. Tip: when your activity has been paused, this activity instance will be kept in the memory and called when the activity needs to be restored. You do not need to reinitialize the component. Restore your activity

When you restore your activity from the paused status, the system calls the onresume () function. You need to know that the activity calls this method every time it enters the foreground, including when the activity is created for the first time. In this case, you must initialize the components released in onpause () in the onresume () method and execute any initialization work required in the resumed state. (For example, starting an animation and initializing the components required for the foreground status of the activity) the following example corresponds to the previous onpause () example. Here, the camera resources released during the pause are initialized.
  1. @ Override
  2. Public void onresume (){
  3. Super. onresume (); // call the parent class Method
  4. // Obtain the camera instance when the activity gets the user focus
  5. If (mcamera = NULL ){
  6. Initializecamera (); // specifies the local method for camera initialization.
  7. }
  8. }
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.