Android exits the application completely. android exits the application.
In Android applications, sometimes we often need to exit the entire application to release resources, instead of simply exiting an Activity. I searched some materials online and combined my own experience, the following examples are more convenient and useful:
Create a SysApplication class, as shown in the following figure. There are many comments.
Package com. llp. dataprocess. util; import java. util. using list; import java. util. list; import android. app. activity; import android. app. application; public class SysApplication extends Application {// used to store all current Activity private lists <Activity> mList = new existing List <Activity> (); // static constants of SysApplication type, used to obtain an instance in an Acitivty and call the class method of SysApplication private static SysApplication instance; private SysApp Lication () {}// static method, used to obtain the static constant instance public synchronized static SysApplication getInstance () {if (null = instance) {instance = new SysApplication ();} return instance;} // add Activity public void addActivity (Activity activity) {mList. add (activity) ;}// Method for exiting the entire application public void exit () {try {for (Activity activity: mList) {if (activity! = Null) activity. finish () ;}} catch (Exception e) {e. printStackTrace ();} finally {System. exit (0) ;}} public void onLowMemory () {super. onLowMemory (); System. gc ();}}
The OnCrate method of each Activity is called as follows:
SysApplication.getInstance().addActivity(this);
Each time an Activity is created, it is added to the List.
To exit the application, call the following method:
SysApplication.getInstance().exit();