Android Learning--exit the entire application

Source: Internet
Author: User

Copy the following Sysapplication class into the project and then pass Sysapplication.getinstance () in the OnCreate method of each acitivity. Addactivity (this); Add the current acitivity to Ancivitylist, and finally call Sysapplication.getinstance () when you want to exit. exit (); You can close all acitivity and exit the application directly.

 PackageCom.example.Emotional.util;Importjava.util.LinkedList;Importjava.util.List;Importandroid.app.Activity;Importandroid.app.Application; /*** class Name: Sysapplication * Class Description: Quit the entire application class * created: 2014-4-25 1:58:00 * Edit notes: *@version    * */  Public classSysapplicationextendsApplication {PrivateList<activity> mlist =NewLinkedlist<activity>(); Private StaticSysapplication instance; Privatesysapplication () {} Public synchronized Staticsysapplication getinstance () {if(NULL==instance) {Instance=Newsysapplication (); }         returninstance; }     //Add Activity     Public voidaddactivity (activity activity) {Mlist.add (activity); }        Public voidexit () {Try {              for(Activity activity:mlist) {if(Activity! =NULL) Activity.finish (); }         } Catch(Exception e) {e.printstacktrace (); } finally{system.exit (0); }     }      Public voidonlowmemory () {Super. Onlowmemory ();     System.GC (); }  }

When the hard Exit button is clicked

//Click the Hard Exit button     Public voidClickquitdialog () {Alertdialog.builder Builder=NewBuilder (firstactivity. This); Builder.setmessage ("Are you sure you want to quit?"); //builder.settitle ("hint");Builder.setpositivebutton ("Confirm",NewAndroid.content.DialogInterface.OnClickListener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Dialog.dismiss ();//FirstActivity.this.finish ();//system.exit (0);sysapplication.getinstance (). exit ();             }                 }); Builder.setnegativebutton ("Cancel",NewAndroid.content.DialogInterface.OnClickListener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)                     {Dialog.dismiss ();             }                 });         Builder.create (). Show (); } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {                          //Event.getrepeatcount () = = 0 Prevent repeated clicks            if(KeyCode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) {clickquitdialog (); return false; }             return false; } 

Android Learning--exit the entire application

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.