Android习惯——给全部Activity添加集合管理

public void ActivityCollector{
	public static List<Activity> activities = 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() 方法即可。

posted @ 2017-03-23 23:23  zh2017  阅读(1502)  评论(0编辑  收藏  举报