Android activity completely ends and exits the instance of the program _android

Source: Internet
Author: User
On the complete exit of the Android program, there are a lot of posts on the Internet, there are many ways. Remember at that time I wrote the first relatively complete project when the problem, because of the activity of the life cycle is not enough to understand, but also use the tabactivity to make the hierarchical relationship is more complex, added in the program "Exit" menu items often can not completely exit, after the various search on the Internet, Various methods have tried, what system.exit (0) Ah, the end of the process, such as Ah, are still ineffective.
This method is discovered later. The idea is also very simple, think back to the original in order to solve this problem when the hard search, then decided to share it for people who encounter this problem as a reference.
Wordy enough, on the train of thought: Add a global variable as a program exit token (Boolean type), when you need to exit the program to True, in the program in each activity in the OnStart method to judge the variable, to true end itself.
Use Application to save a global variable isprogramexit. (for application use unfamiliar words to view relevant information)
Copy Code code as follows:

public class MyApplication Extendsapplication {
Program Exit Tag
private static Boolean isprogramexit = false;
public void Setexit (Booleanexit) {
Isprogramexit= exit;
}

Public Booleanisexit () {
return isprogramexit;
}
}

The OnStart method for each activity that needs to end when you exit adds the following code:
Copy Code code as follows:

Protected Voidonstart () {
Super.onstart ();
MyApplication mApp = (myapplication) getapplication ();
if (Mapp.isexit ()) {
Finish ();
}
}

So C ends up on its own, followed by a process that goes back to B, and then B is judged, because the exit condition satisfies, ends itself, and then goes back to a, also ends ...
As you can see, this approach is not clever or even complicated, because you have to add judgment to the onstart of each activity. But it is undeniable that this method is absolutely effective, regardless of your program in how the activity jumps to jump to go, as long as the command, all end, tried.
Therefore, have encountered the program exits the question friend may serve as the 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.