今天我所要作的笔记是:
当我们的程序需要一个添加、注销或者退出的功能,只需要一个专门的集合类对所有的活动进行管理。代码如下。
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
public class ActivityCollector {
public static Listactivities =new ArrayList<>();
//添加某一个活动
public static void addActivity(Activity activity){
activities.add(activity);
}
//移除某一个活动
public static void removeActivity(Activity activity){
activities.remove(activity);
}
//关闭所有活动
public static void 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){
if (!activity.isFinishing()){
activity.finish();
}
}
activities.clear();
}
}