public class ActivityCollector { public static List<Activity> activities = new ArrayList<Activity>(); 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(); } } } }
public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); ActivityCollector.removeActivity(this); }
<span style="font-family: Arial, Helvetica, sans-serif;">}</span>(3)让A, B, C这三个activity分别去继承BaseActivity.java.这样只要activity一起动,或者子activity中重写了onDestroy()方法,子activity就会自动的去收集activity,去销毁某个Activity.
public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case ConstantUtil.LOGINOUT: ActivityCollector.finishAll(); break;
}
}
如何销毁Activity,和如何一次销毁多个activity
原文地址:http://blog.csdn.net/itbiyu/article/details/42489515