As described below, concentrated in Https://github.com/wytings/CrashDemo
First of all, kill the process or system.exit has actually killed the app, especially if you have only one activity in the stack, and this is a very effective measure. But because of the Android framework, in some cases the app will be restarted, but more accurately, the app has been restored. What's the situation? is if you still have activity that is not actively closed. For example Activitya-open activityb-open-ACTIVITYC, and then Activityc system.exit or Process.killprocess (Process.mypid ()); At this point because the entire app was shut down in C, and A and B but nothing is turned off, the framework layer thinks this is to be killed, so even if we choose to kill, on the current framework of the mechanism, it thinks this kind of active exit is an abnormal exit ... ..., so it will do its best to recover the app.
In the case of "abnormal exit", the framework saves some of the app's state information, such as the activity run stack, and then resumes an Android application, removing the exception activity from the stack, which is equivalent to the back key operation. If there is no activity after removal, then the "Illusion of closure success" is not restored, and if there is activity then the first activity after removal is resumed.
Here we continue to explain in the order in which the above a->b->c is opened:
1, if the ACTIVITYC kill, the app will show activityb when it resumes;
2, if the ACTIVITYB kill process will restore the Activitya;
3, if in Activitya kill process, because there is no previous activity, the program recovery failed, that is, to maintain the dead state.
Since the app is recovering with only one activity, but it retains the information from the previous activity stack, how do I handle the back key? In the first case, we recovered the activityb, but there is a activitya on the activityb, so after the activityb is restored, when we click the back key, we will open Activitya, Although the framework originally wanted to resume Activitya, but it was dead, so can only re-create once. In the same way, you can always go back until all the activity is gone.
The basic information is finished, and we are now beginning to explain the two issues specifically: 1. What are the defects of this abnormal program recovery? 2. How do I close the app completely?
First, we first say that the defect, firstly, if there is only one activity, shut off it seems to be nothing big, after all, an exception. But if there are multiple activity, there is a good chance of a problem after recovery. Why? Because we know that there is only one activity to recover, if your various initialization parameters are not done in application but on one of the loading pages, then the recovery activity is likely to crash by calling some classes that have not been initialized again, This situation will also appear in the press of the Back button, and then continue to restore once, the impression of three times, three times also can not save, the framework will say: forget it, so ...
So, the biggest drawback of this anomaly is that the app is unable to perform the expected initialization process as normal, which adds a lot of uncertainty to subsequent runs.
second, about how to completely close the app. You can look at what Google insiders say and what people are talking about in StackOverflow ...
Https://groups.google.com/forum/#!topic/android-developers/G_D3pKnGLt0
Http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon
Let's cut a picture here.
So, for a single application, there is no ready-to-use "self-closing" feature ... But can you do it? Of course you can do that by catching the exception and then clearing out all the activity and shutting it down completely. This online said to maintain an activity sequence, add a baseactivity, and then in the OnCreate and OnDestroy callback inside save and remove ...
First of all, it is really necessary to maintain a list of activity, but there is no need to do it in the activity, it can be put in application. I wonder if you remember application have a Registeractivitylifecyclecallbacks method? We just need to do this in application, as shown in:
Then, you can ~ the above, but also have a folk prescription ...
The above mentioned, concentrated in Https://github.com/wytings/CrashDemo
Android troubleshooter killprocess and System.exit invalid