Activity is managed by the activity stack, and when it comes to a new activity, the activity is added to the top of the activity stack, and the activity is at the bottom of the activity. There are four states in the general sense of acitivity:
1. When the activity is at the top of the stack, it is in the front of the screen at this time and is in a running state ;
2. When the activity loses focus but remains on the visible (such as the top of the activity is transparent or the top of the stack activity is not covered with the entire phone screen), at this time in a paused state ;
3. When activity is completely obscured by other activity, the activity is not visible to the user at this time and is in a stopped state ;
4. When the activity is destroyed due to human or system reasons (such as low memory, etc.), it is in the destruction state at this time;
In each of the different state stages, the adnroid system callbacks the corresponding methods within the activity. Therefore, when we write activity in the program, we generally inherit the activity class and rewrite the corresponding callback method.
First put a picture from the Official document (http://developer.android.com/reference/android/app/Activity.html), I believe you have seen.
The diagram details the entire lifecycle of the activity and the corresponding callback methods during different states.
Here are a few things to look for in the diagram:
1.Activity instances are created automatically by the system and callback the corresponding method during different states. One of the simplest complete activity lifecycles will be recalled in the following order: OnStop, OnPause, Onresume, OnStart, OnCreate, and so on. Called entire lifetime.
2. When the OnStart callback method is executed, the activity starts to be seen by the user (that is, the user does not see the activity when OnCreate, which one does the user see?). Of course the activity before this activity), until OnStop, this stage of activity is visible to the user, known as the visible lifetime.
3. When the Onresume callback method is executed, the activity can respond to user interaction until the OnPause method is called foreground lifetime.
In a real-world scenario, a activity is assumed to be at the top of the stack, where the user operates, jumping from a activity to B activity. So what are some of the life cycle methods that will be recalled for AB? What is the specific callback order of the callback method?
At the beginning, A is instantiated and the callback executed is A:oncreate, A:onstart, A:onresume.
When the user clicks a in the button to B, assuming that B is all blocked a, will be executed A:onpause, b:oncreate, B:onstart, B:onresume, A:onstop.
At this point, if you click the back key, you will perform B:onstop, B:ondestroy, A:onresume, A:onstart, B:onpause, A:onrestart, and so on.
At this point, only a is in the activity stack. In Android, there are two keys that affect the activity life cycle in particular, the back key and the home key. Let's look directly at the results of the experiment:
If you press the back key at this point, the system returns to the desktop, followed by A:onpause, A:onstop, A:ondestroy.
If you press the home key (not the long Press), the system returns to the desktop and executes A:onpause-a:onstop in turn. Thus, the main difference between the back key and the home key is whether the OnDestroy will be executed.
At this point, if you press and hold the home button, different phones may pop up different content, the activity life cycle has not changed (measured by millet 2s, do not know whether the other phone will affect the activity life cycle).
Because of the characteristics of Android itself, so many applications now do not directly exit the function of the application, according to the general logic, when there is only one activity in the activity stack, when you press the Back button this activity will execute OnDestroy, Then next click on this app icon will be restarted, so, many applications are currently taking the effect of the home button, when the Back button is clicked, the system returns to the desktop, and then click the application icon, directly back to the previous activity interface, how this effect is implemented?
By overriding the callback function that presses the back key, you can turn the effect on the home key.
@Overridevoid onbackpressed () { new Intent (Intent.action_main); Home.addcategory ( Intent.category_home); StartActivity (home);}
Of course, the effect of the back key on the activity life cycle is influenced by the home key effects in this way. Note that this method only overrides the activity when the back key needs to be returned to the desktop and the home effect is reached.
Or, to achieve such an effect, the activity actually provides a direct method.
1 Activity.movetasktoback (true);
Movetasktoback () This method moves the task where the current activity resides directly to the background while preserving the activity order and state.
In the previous project development process, encountered a very strange problem: "developer Options" on the phone has a "do not keep active" setting, when this setting is turned on, the settings on the phone is "user left to destroy each activity", after opening, For other applications that are Activity from a acticity to B, and then back to a, the other application is just white screen (possibly black screen, depending on the theme setting), and a starts to be visible, but one result in my application is to return directly to the desktop. At first baffled. Finally, the problem was fixed. First, we need to be clear about the impact on the activity life cycle when this setting is turned on. When This setting is turned on, when a to B is assumed, B is all obscured by a, it will be executed in turn a:onpause, B:oncreate, B:onstart, B:onresume, A:onstop Estroy. Yes, a OnDestroy has been added to the system's original life-cycle callback. This is the meaning of " destroy each activity after the user leaves ". However, it is important to note that, as long as there is no thought of calling the finish () method of a, although a executes the OnDestroy, the activity stack still retains a, at which point B is at the top of the stack. Then press the back key in B to return to A, will be executed:b:onpause, A:oncreate, A:onstart, A:onresume, B:onstop. Yes, A has been executed since OnCreate. It also explains why a may appear white screen (or black screen, etc.) for a moment.
So why is my application different from other applications? Finally, the problem was that in my application, I used an activity stack to maintain the activity in order to completely quit the application, and now think about it, It is not necessary, and do not say that the characteristics of the Android itself decided not to use such a method to achieve the exit effect, just this method itself is also a big problem, and now on the internet can still see a lot of articles on the application exit can use this method, hey. ), OnCreate in the stack, OnDestroy out of the stack, called the following methods :
End activity& Remove 2 Appmanager.getappmanager () from the stack. Finishactivity (this);
Where the finishactivity function in AppManager is specifically defined as:
1/**2 * end the specified activity 3 */< Span style= "color: #008080;" > 4 public void 5 if (Activity! = Span style= "color: #0000ff;" >null) { 6 Activitystack.remove (activity); Activity.finish (); null; 9 }10}
At this point, I believe you should see the problem lies in it.
Yes, the problem is that the finish () method of the activity is executed!! The finish () method of the activity has at least two levels of meaning, 1. Remove this activity from the activity stack, 2. The OnDestroy method for this activity is called. There is no real effect on setting items that do not keep active, but once you turn this setting on, the problem is obvious. When this setting is turned on, it normally leaves a, even if there is a in the ondestroy,activity stack that executes a, but when I write this, the finish () method executes, and there is no a in the activity stack, so when you click the back key, There is no activity in this app in the activity stack, and it comes directly to the mobile desktop.
Maybe, some people would say, I just want to use this method to completely exit the application, while I want my activity stack and the activity stack in the system to be consistent, how to do?
Here, you can do this by rewriting the following:
/*** ends the specified activity */public void Finishactivity (activity activity) {if (Activity! = // To be consistent with the system activity stack, and given the problem caused by the activity Lifecycle Call OnDestroy () method due to the "do not retain activity" option in the phone settings, the following corrections are required here if (Activity.isfinishing ()) {Activitystack.remove ( activity); //activity.finish (); activity = null
Keep this in mind!
-------------------------------------------------
In addition, for different startup modes or intent flags or operational behavior (such as portrait-to-landscape switching), such issues may affect the activity life cycle, and such questions will be summarized in subsequent related articles.
Reprint: Corn http://www.cnblogs.com/lwbqqyumidi/p/3769113.html
Android Activity life Cycle