来自:第一行代码
1 public class ActivityCollector { 2 public static List<Activity> activities = new ArrayList<Activity>(); 3 public static void addActivity(Activity activity) { 4 activities.add(activity); 5 } 6 public static void removeActivity(Activity activity) { 7 activities.remove(activity); 8 } 9 public static void finishAll() { 10 for (Activity activity : activities) { 11 if (!activity.isFinishing()) { 12 activity.finish(); 13 } 14 } 15 } 16 }
1 public class BaseActivity extends Activity { 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 Log.d("BaseActivity", getClass().getSimpleName()); 6 ActivityCollector.addActivity(this); 7 } 8 @Override 9 protected void onDestroy() { 10 super.onDestroy(); 11 ActivityCollector.removeActivity(this); 12 } 13 }