写一个baseactivity,在其onCreate方法中调用addActivity方法
在其onDestroy方法中调用removeActivity方法
在需要退出应用程序的地方调用finishAll方法
public class ActivityCollector{
public static List<Activity> activities = new ArrayList<>();
//添加活动
public static void addActivity(Activity activity){
activities .add(activity);
}
//删除活动
public static void removeActivity(Activity activity){
activities .remove(activity);
}
//销毁所有活动
public static void finishAll() {
for(Activity activity: activities){
activity.finish
}
}
}