Android completely quits application, perfect solution

Source: Internet
Author: User

Recently, the company is not very busy, the time to study the next Android Guide page, but after the completion of the Guide page and enter the live page, in the exit, the use of "Press again exit" way to achieve, the way is to kill the process and exit, namely: Android.os.Process.killProcess (Android.os.Process.myPid ()) or system.exit (0);

But the problem arises because I have a welcome page activity before I go into the Guide page activity and the main interface activity (Note: This entry activity is to determine whether the application is the first time the load, if it is the first time to load jump to the boot page, otherwise, Jump directly to the main interface activity), when I use either of the two methods, it does not completely quit the application, but simply destroy the current activity and return to the previous activity, that is, the Portal Welcome page activity, But this is not the result I want, so I looked up the solution to completely exit the application, finally found the solution, the perfect solution to this problem, hoping to learn from the learning of Android dishes birds ...

Android Exit application callback with Android.os.Process.killProcess (Android.os.Process.myPid ()) or system.exit (0), which is only valid for the first activity (That is, the import welcome page activity), this scenario cannot be implemented if more than one activity is required in my application and requires that each activity be able to exit the application directly.

On the Web, the most popular way is to define a stack, write a custom MyApplication class, and use singleton mode to manage the activity individually, in each activity's OnCreate () Method calls Myapplication.getinstance (). Addactivity (this) adds the current activity to the stack for unified management, If you need to exit the application, call Myapplication.getinstance (). The exit () method exits the application directly.

Implementation: The benefit of unified management is that if you need to exit, directly add the activity of the stack to agree to finish on the line. The exit method is implemented by looping all the activity instances in the stack and then the finish.

The specific invocation method as described above, the following is only a concrete implementation of MyApplication, as to how to call here is inconvenient to repeat.

The code is as follows:

public class MyApplication extends application {

Private list<activity> activitylist = new linkedlist<activity> ();    private static exitapplication instance; Private Exitapplication () {}//in singleton mode gets the unique exitapplication instance public static Exitapplication getinstance () {if (   instance = = NULL) {instance = new exitapplication ();  } return instance;  }//Add activity to the container public void addactivity (activity activity) {This.activityList.add (activity);   } public void Exit () {for (Activity activity:this.activityList) {activity.finish ();  } system.exit (0); } }

Android completely quits application, perfect solution

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.