Android活动管理工具

时间:2024-03-25 08:33:32

ActivityCollector.java

import android.app.Activity;
import java.util.ArrayList;
import java.util.List; //活动汇总管理
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<Activity>(); //添加活动
public static void addActivity(Activity activity) {
if (!activities.contains(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();
}
}
} }

BaseActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log; //重写Activity基本类
public class BaseActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//显示父类名称
Log.d("woider", getClass().getSimpleName());
//添加至活动中心
ActivityCollector.addActivity(this);
}
protected void onDestroy(){
super.onDestroy();
//从活动中心移除
ActivityCollector.removeActivity(this);
} }