Pause and resume Activity Android and activityandroid

Source: Internet
Author: User

Pause and resume Activity Android and activityandroid

Pause and resume an Activity (Pausing and Resuming an Activity)

When used in a normal application, the foreground activity is sometimes blocked by other visualization components, resulting in the suspension of the activity. For example, when a translucent activity is opened (for example, in a style dialog box), the previous activity is paused. As long as the activity is still partially visible but does not get the focus, it is still paused.

However, once the activity is completely blocked and invisible to the user, it stops (this is the content to be discussed in the next lesson ).

When your activity is paused, the system will call the onPause () method for your Activity. In this method, you can stop behaviors (such as video playback) that should not be continued during pause, or persist information that needs to be permanently saved, to keep the user in your application. If the user returns to your activity, the system will restore it from the paused state and call 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 indication that the user is leaving your activity.

Figure 1. When a translucent activity masks your activity, the system will call onPause (), activity, etc. in the paused state (1 ). If the user returns an activity that is still paused, the system will call onResume () (2 ).

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 is that the user is leaving the activity, and it will soon enter the Stopped state. You should usually use onPause () callback:
Stop the animation effect or other actions that consume CPU resources in progress.

Submit unsaved changes, provided that the user wishes to do so: When they leave, they need to save the changes permanently (such as writing an email ).
Release some system resources, such as broadcast receivers, processing sensors (such as GPS), or any resources that will affect battery life, and your activity is paused at the same time, you no longer need these resources.

For example, if your application uses a camera, 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;    }}

Generally, you should not use onPause () to save user changes (such as personal information input format) to persistent storage. You can use onPause () only when you confirm that the user expects to automatically save the changes (such as drafting an email () method To write this change to persistent storage. However, you should avoid performing CPU high-load work in onPause (), such as reading and writing the database, because it will slow down the transition to the next activity (you should stop in onStop () ).

You should keep the business volume in the onPause () method relatively simple. To make 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 exists in the memory and is re-called when the activity is restored. When you call any callback method to change to the recovery status, you do not need to reinitialize those created components ,.

Restore Your Activity (Resume Your Activity)

The onResume () method is called when you resume your activity from the pause state.

Note that the system will call this method every time your activity enters the foreground, including when it is created for the first time. Therefore, when implementing onResume (), you should initialize those components that will be released in onPause, and execute the initialization actions that must be completed every time the activity enters the recovery State (such as the components required to start the animation and initialize the activity to get the user focus ).

In the following example, onResume () is the corresponding onPause () above, so it initializes the camera released when the activity is 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    }}

Android activity exit

Use System. exit (0); to exit the program directly.
Finish (); destroys the current activity;

The thread suspended by android (suspend) is invalid.

Android does not support suspend, wait, policy, and other functions. android considers these functions unsafe and thus disabled.
Try to replace sleep

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.