Android Press once again to completely exit the program code

Source: Internet
Author: User

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

QQ Technology Group: 129518033

Objective:

The exit program used earlier uses finish (), which only exits the current activity. If you have more than one activity, you can't quit at once.


1. Exit the Application Tools class

/** *  * @author itas109 * TODO is used to completely exit the application  */public class Exitapputils extends application {    private List<ac tivity> activitylist = new linkedlist<activity> ();    private static exitapputils instance;      Private Exitapputils () {    }      //In singleton mode gets the unique Exitapputils instance public    static Exitapputils getinstance () {        if ( NULL = = Instance) {            instance =new exitapputils ();        }        return instance;    }      Add activity to container public    void addactivity (activity activity) {        activitylist.add (activity);    }      Traverse all Activity and finish public      void exit () {for        (Activity activity:activitylist) {            activity.finish (); c21/>}        system.exit (0);}    }

2. How to use

Add the following code to each activity's OnCreate method

Exitapputils.getinstance (). addactivity (this);


3. Achieve a full exit effect by pressing again

Define Time

Private long exittime = 0;
Exit the code and put it in the activity you want to exit.

public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back && event.getaction () = = Keyevent.action_down) {if ((System.currenttimemillis ()-exittime) >) {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 want to reprint please indicate the source: http://blog.csdn.net/itas109

QQ Technology Group: 129518033

Android Press once again to completely exit the program code

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.