Android completely exits the program code again, and the android program code

Source: Internet
Author: User

Android completely exits the program code again, and the android program code

If you need to reprint please indicate the source: http://blog.csdn.net/itas109

QQ technology exchange group: 129518033

Preface:

The previously used exit program uses finish (), which can only exit the current Activity. If there are more than one Activity, you cannot exit at a time.


1. Exit the application tool class.

/***** @ Author itas109 * TODO is used to exit the Application completely */public class ExitAPPUtils extends Application {private List <Activity> activityList = new shortlist <Activity> (); private static ExitAPPUtils instance; private ExitAPPUtils () {}// obtain the unique ExitAPPUtils instance public static ExitAPPUtils getInstance () {if (null = instance) in singleton Mode) {instance = new ExitAPPUtils ();} return instance;} // Add the Activity to the container public void addActivity (Activity activity) {activityList. add (activity);} // traverse all activities and finish public void exit () {for (Activity: activityList) {activity. finish ();} System. exit (0 );}}

2. Usage

Add the following code to the onCreate method of each Activity:

ExitAPPUtils.getInstance().addActivity(this);


3. implement full exit once again

Define time

private long exitTime = 0;
Exit code and put it in the Activity to be exited.

public boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){ if((System.currentTimeMillis()-exitTime) > 2000){ ToastUtils.show(getApplicationContext(), R.string.exit_hint);exitTime = System.currentTimeMillis(); } else {ExitAPPUtils.getInstance().exit();//finish();//System.exit(0);}return true; }return super.onKeyDown(keyCode, event);}}

4,



If you need to reprint please indicate the source: http://blog.csdn.net/itas109

QQ technology exchange group: 129518033

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.