When writing an Android application, you will often encounter a need 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 directly to return, in fact, neither of these actions closes the current application and does 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.
There are several methods available on the Web, and I have absorbed the following self-explanatory method, which will be sysapplication This class is copied into the project, and then in each acitivity OnCreate method through the sysapplication.getinstance (). addactivity (this);   Add the current acitivity to Ancivitylist and finally call sysapplication.getinstance (). exit (); You can close all acitivity and exit the application directly.
Attached code:
Import java.util.LinkedList; Import java.util.List; Import android.app.Activity; Import Android.app.AlertDialog; Import android.app.Application; Import Android.content.DialogInterface; Import android.content.Intent; public class Sysapplication extends application {private list<activity> mlist = new Linkedlist<activity> ( ); private static sysapplication instance; Private Sysapplication () {} public synchronized static Sysapplication getinstance () {if (null = = Insta NCE) {instance = new sysapplication (); } return instance; }//Add activity public void addactivity (activity activity) {Mlist.add (activity); } public void exit () {try {for (activity activity:mlist) {if (Activity! = Nu ll) Activity.finish (); }} catch (Exception e) {e.printstacktrace (); } finally {system.exit (0); }} public void Onlowmemory () {super.onlowmemory (); System.GC (); } }
add inside the oncreate of the activity inside the applicationsysapplication.getinstance (). Addactivity (This)
such as:
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Sysapplication.getinstance (). addactivity (this); }
android--exiting the entire application