An Effective Method for Android to end the activity and exit the program

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.