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