How to completely exit the Android app

Source: Internet
Author: User

When an Android application contains multiple activity, to completely exit the Android application, you want to destroy all the activity, the following is an online spread of the more classic perfect way:

First, you define a class that inherits application, with the following code and comments:

The MyApplication class is used to store each activity and to close all activity operations
public class MyApplication extends application {

Defining Activity Containers
Private List activitylist = new LinkedList ();
private static MyApplication instance;

Private MyApplication () {}
Unique MyApplication instances in a singleton design pattern
public static MyApplication getinstance () {
if (instance = = null)
Instance = new MyApplication ();
return instance;
}
Adding activity to the container
public void addactivity (activity activity) {
Activitylist.add (activity);
}
Traverse all the activity and finish
public void Exitapp () {
for (Activity activity:activitylist) {
if (activity! = NULL)
Activity.finish ();
}
System.exit (0);
}
Empty cache
@Override
public void Onlowmemory () {
Super.onlowmemory ();
System.GC ();
}


}

Next, add the following code to each activity's OnCreate method:

Myapplication.getinstance (). addactivity (this);

Add the activity to the activity container.

Finally, add the following code where you are implementing the exit application:

Myapplication.getinstance (). Exitapp ();

How to completely exit the Android app

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.