About completely exiting androidProgramThere are many posts and methods on the Internet. I remember that I encountered this problem when I wrote the first complete project. Because I didn't know enough about the lifecycle of the activity, I used tabactivity to make the hierarchy complex, the "exit" menu item added in the program often cannot be completely exited. After that, various searches on the Internet and methods have been tried. exit (0), process termination, and so on, are still ineffective.
This method is only found later. The idea is also very simple. When I think back to my hard work in order to solve this problem, I decided to share it for reference.
Long enough, the idea is: Add a global variable as the flag for program exit (boolean type), and set it to true when the program needs to exit, this variable is determined in the onstart method of each activity in the program. If it is true, it ends itself.
ViewCodeRight:
Use application to save a global variable isprogramexit. (If you are not familiar with application, view relevant information)
Public class myapplication extends application {
// Exit flag
Private Static Boolean isprogramexit = false;
Public void setexit (Boolean exit ){
Isprogramexit = exit;
}
Public Boolean isexit (){
Return isprogramexit;
}
}
Add the following code to the onstart method of each activity to end when you exit:
Protected void onstart (){
Super. onstart ();
Myapplication Mapp = (myapplication) getapplication ();
If (MAPP. isexit ()){
Finish ();
}
}
For example, there are three activities in the program, a -- B -- C; now there is a button in C, click to exit the entire program. As mentioned above, the judgment code is added to the onstart methods of A and B (which can be left blank in C), and executed when the button is clicked in C:
Myapplication Mapp = (myapplication) getapplication ();
Mapp. setexit (true );
Finish ();
In this way, C will end itself, return to B according to the process, and then judge in B, because the exit condition is met, and end itself; then return to A, and it will also end ......
It can be seen that this method is not clever, or even complicated, because it must be judged in the onstart of each activity. However, it is undeniable that this method is absolutely effective. No matter how the activity jumps in your program to jump to the next hop, it can be tried and tested once and for all.
Therefore, a friend who has encountered a program exit problem can serve as a reference.