Suspend and resume activity (pausing and resuming)
An activity is an application component that provides a screen that users can use to interact in order to accomplish a task, such as dialing, taking photos, sending emails, and looking at a map. Each activity is given a window in which the user interface can be drawn. Windows are usually filled with screens, but they can also be smaller than the screen and float on top of other windows.
An application is usually composed of multiple activities, usually loosely coupled relationships. Typically, an activity in an application is designated as the "main" activity, the activity that is presented to the user the first time the application is started. Each activity can then be activated by another action in order to accomplish different actions. Each time an activity starts, the previous activity stops, but the system retains the activity on one stack ("Back stack"). When a new activity starts, it is pushed to the top of the stack to get the user focus. The back stack conforms to the simple "LIFO" principle, so when the user completes the current activity and then clicks the return button, it is ejected from the stack (and destroyed) before the activity resumes.
When an activity stops because of a new activity initiation, it is notified that the state transition passes through the activity's lifecycle callback function. There are many callback functions where an activity may be received, originating from its own state change-whether the system creates it, stops it, restores it, destroys it-and each callback provides you with the opportunity to complete a designated job that fits the state. For example, when you stop, your activity should release any large objects, such as a network database connection. When the activity resumes, you can regain the necessary resources and resume the interrupted action. These state transitions are part of the life cycle of an activity.
In normal applications, the foreground activity is sometimes obscured by other visual components, causing the activity to pause. For example, when a semitransparent activity is opened (as in a style dialog), the previous activity is paused. As long as the activity is still partially visible, but the focus is not currently available, it is still in a paused state.
However, once the activity is completely blocked and not visible to the user, it stops (this is what the next lesson needs to be discussed).
When your activity enters a paused state, the system will call the OnPause () method on your activity. In this approach, 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 permanently saved so that the user stays in your application. If the user returns to your activity, the system restores it from the paused state and invokes 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 users are leaving your activity.
Figure 1. When a translucent activity obscures your activity, the system invokes OnPause (), activity, etc. in a paused state (1). If the user returns to an activity that is still in a paused state, the system invokes Onresume () (2).
Suspend your activity (Pause Your activity)
When the system calls OnPause () for your activity, your activity is technically still partially visible, but the most common indication is that the user is leaving the activity and that it will soon be in a stop (Stopped) state. You should
The OnPause () callback is typically used:
Stop animation effects or other behavior that is consuming CPU resources.
Commit changes that have not yet been saved, but only if the user wishes to do so: when they leave, they need to make a permanent save (such as composing an e-mail message) of the changes made.
Release some system resources, such as broadcast receivers, process sensors (such as GPS), or any resources that affect battery life, while your activity is suspended and 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.
@Override public
void OnPause () {
super.onpause ();//Always Call the Superclass method I
//release the C Amera because we don ' t need it when paused//and other activities might 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, into persistent storage. You can write a user's change to a persistent store in the OnPause () method only if you are certain that the user is expecting to automatically save his or her changes (for example, by drafting an e-mail message). However, you should avoid performing CPU workloads in OnPause (), such as reading and writing databases, because it slows down the transition to the next activity (you should perform a heavy shutdown in OnStop ()).
You should keep the volume of business in 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 called back when the activity resumes. You do not need to reinitialize the components that have already been created when you call any of the callback methods into the recovery state.
Resume your activity (Resume Your activity)
When a user resumes your activity from a paused state, the system calls the Onresume () method.
Note that every time your activity enters the foreground, the system calls this method, including when it is first created. So, when you implement Onresume (), initialize the components that you will release in OnPause (), and perform those every activity Initialization actions that must be completed when entering the recovery state (such as the components that need to be used to start the animation and initialize the activity to gain user focus).
The following example Onresume () is the example above for the corresponding OnPause (), so it initializes the camera that was released when the activity was paused.
@Override public
void Onresume () {
super.onresume ();//Always Call the Superclass method a
/Get the Cam Era instance as the activity achieves full user focus
if (Mcamera = null) {
initializecamera ();//local method T O Handle camera init
}
}
The above is a small series to introduce the Android activity suspension and recovery of the relevant knowledge, hope to help you!