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