自定义一个继承自Application的类。用来记录所打开的Activity。
在每个Activity类的onCreate方法中调用ExitApplication.newInstance().addActivity(this);将Activity类添加到list中。
在退出时调用ExitApplication.newInstance().exit();循环执行finish();
public class ExitApplication extends Application {
private static List<Activity> acList = new LinkedList<Activity>();
private static ExitApplication instance = null;
private ExitApplication(){}
//单例模式
public static ExitApplication newInstance(){
if(null==instance){
instance = new ExitApplication();
}
return instance;
}
public void addActivity(Activity a){
acList.add(a);
}
public static void exit(){
for (Activity activity : acList) {
activity.finish();
}
System.exit(0);
}
}
时间: 2024-10-06 13:10:46