Android exits the application completely. android exits the application.

Source: Internet
Author: User

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();

Related Article

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.