Ext.: http://blog.csdn.net/sunnyfans/article/details/7688092
When we write an Android application, we often encounter the desire to exit the current acitivity, or exit the application directly. My previous general operation is to press the back key, or directly press the home key to return directly, neither of these operations have closed the current application, and did not release the system resources. Sometimes jump to more activity, you also need to press the return key several times, so feel a bit uncomfortable.
After adding a menu return function key, this method can only use System.exit (0) to close the active activity, with the code:
public boolean Oncreateoptionsmenu (Menu menu) { super . Oncreateoptionsmenu (menu); MenuItem Item = Menu.add (Menu.none, Menu.none, Menu.none, "Exit" ); Item.setonmenuitemclicklistener ( new Menuitem.onmenuitemclicklistener () { public boolean Onmenuitemclick (MenuItem item) {system.exit ( 0 return true return true ;}
It is too inconvenient to think of this, every time the application is still running after exiting the interface, today checked the information to finally solve the problem,
There are several methods available on the Web, and I have absorbed the following self-explanatory method to copy the following sysapplication into the project, Then in each acitivity OnCreate method through Sysapplication.getinstance (). 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.
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 (); } }
Add Sysapplication.getinstance () to the oncreate inside the activity inside the application. Addactivity (This)
Such as:
Public void onCreate (Bundle savedinstancestate) {superonCreate (savedinstancestate); Setcontentview ( R.layout.main); Sysapplication.getinstance (). addactivity (this); }
Android uses singleton mode to exit the entire application (GO)