Android uses singleton mode to exit an entire application

Source: Internet
Author: User

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)

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.