title: 2017-3-23 Android习惯——给全部Activity添加集合管理
public void ActivityCollector{ public static Listactivities = new ArrayList<>(); public static addActivity(Activity activity){ activities.add(activity); } public static removeActivity(Activity activity){ activities.remove(activity); } public static finishAll(){ for(Activity activity : activities){ if(!activity.isFinishing()){ activity.finish; } } }}
然后在其它活动中的生命周期里添加,比如:
... onCreate(...){ ... ActivityCollector.addActivity(this); } ...onDestroy(...){ ... ActivityCollector.remove(this); }
再比如,不管你在哪个 Activity 想退出程序,只要调用 finishAll() 方法即可。