public class ActivityManager { public static List<Activity> activityList = new ArrayList<Activity>(); public static void addActivity(Activity activity){ activityList.add(activity); } public static void removeActivity(Activity activity){ activityList.remove(activity); } public static void finishAllActivity(){ for(Activity activity:activityList){ if(!activity.isFinishing()){ activity.finish(); } } } }
在Activity中的onCreate()方法中使用:ActivityManager.addActivity(this);将活动添加到List中,在onDestroy()方法中使用:ActivityManager.removeActivity(this);将Activity从List中移除,在想退出整个应用的地方使用:ActivityManager.finishAll()销毁所有Activity并退出应用。