Android official Getting Started document [13] pausing and resuming an activity

Source: Internet
Author: User

Android official Getting Started document [13] pausing and resuming an activity
Pausing and Resuming an Activity
Pausing and resuming an activity

This lesson teaches
1.Pause Your Activity
2.Resume Your Activity

You should also read
? Activities
This lesson teaches you
1. Suspend your Activity
2. Resume your Activity

You should also read
? Activity activities

Try it Out
Give it a try.

Download the Demo
Activitylifecycle.zip
Download Demo
Activitylifecycle.zip

During Normal app use, the foreground activity was sometimes obstructed by other visual components that cause the activity To pause. For example, when a semi-transparent activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the activity is still partially visible but currently isn't the activity in focus, it remains paused.
During normal application use, the foreground activity is sometimes hindered by visible parts such as the activity suspension. For example, when a semi-transparent activity is opened (such as a way in a conversation), the previous activity activities are paused. As long as activity activities are still partially visible, but are not currently in focus, activity activities remain paused.

However, once the activity is fully-obstructed and not visible, it stops (which are discussed in the next lesson).
However, once the activity is completely blocked and invisible, it stops (this will be discussed in the next lesson).

as your activity enters the paused state, the system calls the OnPause () method on your A Ctivity, which allows you to the stop ongoing actions, should not continue while paused (such as a video) or persist any I Nformation that should is permanently saved in case the user continues to leave your app. If the user returns to your activity from the paused state, the system resumes it and calls the Onresume () method.
for your activity to go into a paused state, the system calls the activity of the OnPause () method, which allows you to stop the action that should not continue while pausing (such as video) or continue to be persisted in any case, the information user continues to leave your application. If the user returns to activity from the paused state, the system recovers and calls the Onresume () method.

Note:when your activity receives a call to OnPause (), it is a indication that the activity would be paused for a mome NT and the user may return focus to your activity. However, it's usually the first indication that's the user is leaving your activity.
Note: When your activity is receiving a call to OnPause (), it may be a sign that the activity will be paused for a while and the user can focus back to your activity. However, this is usually the first indication that the user is leaving your activity.

Figure 1. When a semi-transparent activity obscures your activity, the system calls OnPause () and the activity waits in the Paused s Tate (1). If the user returns to the activity while it ' s still paused, the system calls Onresume () (2).
Figure 1: When a translucent activity obscures your activity, the system calls the OnPause () and the inactive activity waits (1). If the user returns to the activity while it is still paused, the system calls Onresume () (2).

Pause Your Activity
Suspend your Activity


--------------------------------------------------------------------------------

when the system calls OnPause () for your activity, it technically means your activity is Still partially visible, but most often is a indication that the user is leaving the activity and it'll soon enter the Stopped state. You should usually with the OnPause () callback to:
? Stop animations or other ongoing the actions that could consume CPU.
? Commit unsaved changes, but only if the users expect such changes to being permanently saved when they leave (such as a draft EMA IL).
? Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources the may affect bat Tery life While your activity was paused and the user does not need them.
When the system calls the OnPause () for your activity, which technically means that your activity is still partially visible, but most of the time it shows the user leaving the activity, it will quickly go into a stop state. Usually you should use the OnPause () callback to:
? Stop the animation or may consume other ongoing actions on the CPU.
? Commit unsaved changes, but only if the user wants such a change when they leave (such as an e-mail draft) to be permanently saved.
? release system resources such as broadcast receivers, handle sensors (such as GPS), or may affect battery life while your activity activities will be paused and users will not need any of their resources.

For example, if your application uses the Camera, the OnPause () method was a good place to release it.
For example, if your application uses a camera, the OnPause () method is a good place to release it.

@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 don't use OnPause () to store user changes (such as personal informati On entered into a form) to permanent storage. The only time you should persist user changes to permanent storage within onPause () are when you ' re certain users expect th E changes to is auto-saved (such as when drafting an email). However, should avoid performing cpu-intensive work during onPause (), such as writing to a database, because it can SL ow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop ()) .
Typically, you should not use OnPause () to store user changes (such as entering a form of personal information) to permanent storage. You should insist that in OnPause () the user changes to persistent storage is only when some of your users expect changes that are automatically saved (for example, when drafting an e-mail message). However, you should avoid performing CPU-intensive work during onpause (), such as writing to the database, as it can slow the visible transition to the next activity activity (you should not perform a high-load shutdown operation during onstop ()).

You should keep the amount of operations do in the OnPause () method relatively simple in order to allow for a speedy Tra Nsition to the user's next destination if your activity is actually being stopped.
You should keep in order to allow for a rapid transition to the user's next destination if your activity is actually stopping at the amount of OnPause () method that is relatively simple to operate.

Note:when your activity is paused, the activity instance are kept resident in memory and are recalled when the activity res Umes. You don ' t need to re-initialize-were created during any of the callback methods leading up to the resumed State.
Note: When your activity is paused, the activity instance remains in memory while the activity activities are resumed. You do not need to re-initialize any of the callback methods in the process to cause the component to be created in the continuation state.

Resume Your Activity
Resume your Activity


--------------------------------------------------------------------------------

When the user resumes your activity from the Paused state, the system calls the Onresume () method.
The system calls the Onresume () method when the user resumes the activity from the paused state.

Be aware that the system calls this method every time your activity comes to the foreground, including when it ' s created For the first time. As such, you should implement Onresume () to initialize, the release during OnPause () and perform any other Initializations that must occur each time the activity enters the resumed state (such as begin animations and initialize C Omponents only used while the activity has user focus).
Note that the system calls this method every time your activity enters the foreground, when it is first created to include times. Therefore, you should implement Onresume () to initialize your OnPause () during the release of the component and perform any other initialization that must occur each time the activity enters the recovery state (such as starting the animation and initializing the component only for the activity that has user focus).

The following example of Onresume () is the counterpart to the OnPause () example above, so it initializes the camera that ' s Released when the activity pauses.
The following example of Onresume () corresponds to the above example of the OnPause (), so it is initialized when the activity is released to suspend the camera.

@Override
public 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
}
}


Next:stopping and restarting an Activity
Next: Stop and restart activity activities

This article is translated from: https://developer.android.com/training/basics/activity-lifecycle/pausing.html

Android official Getting Started document [13] pausing and resuming an activity

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.