Android Perfect Exit App (exit)

Source: Internet
Author: User

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:

    1. KillProcess, this way, when you kill, the activity returns to the previous activity
    2. Android Level 8 (including 8) before using an API to operate, Level8 later is another, so not universal
    3. Use Flag_activity_clear_top, from A to B
Here's how it works: we all know that the activity of Android is a historical 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. [Java]View PlainCopy
  1. Public class Agentapplication extends application {
  2. Private list<activity> activities = new arraylist<activity> ();
  3. public void addactivity (activity activity) {
  4. Activities.add (activity);
  5. }
  6. @Override
  7. public void Onterminate () {
  8. super.onterminate ();
  9. For (Activity activity:activities) {
  10. Activity.finish ();
  11. }
  12. OnDestroy ();
  13. System.exit (0);
  14. }
  15. }

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 appears 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.

Android Perfect Exit App (exit)

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.