This article transferred from: http://blog.csdn.net/zeus_9i/article/details/7259881
The last two days in order to solve the Android top exit program problem toss half dead, in Google & Baidu above looked for a long time, a very long time out of the complete uniformity, said the method has three, but after I test all not.
The three methods were:
- KillProcess, this way, when you kill, the activity returns to the previous activity
- Android Level 8 (including 8) before using an API to operate, Level8 later is another, so not universal
- Use Flag_activity_clear_top, from A to B
here's how to do this:we all know that the activity of Android is stored in the history stack, for example, from a to B C, C, after finishing the finish to B, all the activity is finished, the program will naturally quit. Of course, in the finish at the same time also need to do their own program of other resources. So we need to think of a way to save up the Activity. Then call their finish () method where the program exits. use global variables. By the way, the first thing to think of is to inherit application, code into the next.
public class Agentapplication extends application {private list<activity> activities = new arraylist<activity& gt; ();p ublic void addactivity (activity activity) {Activities.add (activity);} @Overridepublic void Onterminate () {super.onterminate (); for (Activity activity:activities) {activity.finish ();} OnDestroy (); System.exit (0);}}
then when the activity onCreate to call Addactivity (), one might think that this application need to be used in all activity onCreate, need to make a singleton example. I don't really need it at all. You can use This.getapplication () in your Activity. finally call Application.onterminate () in the place where you need to launch the program. Remember: super.onterminate () must be called, OnDestroy () in the code is my own method of releasing other resources, not the system. after running the above code, a line of hints will appear in Logcat:The Process package name (PID xxxxx) has died. Prove that your program has exited. Now you can test it. hope to be useful to everyone. At the same time thank colleagues Hu help me deal with other problems, so that I have time to write a blog, thank you
Go Android Perfect Exit App (exit)