1.BaseActivity
如何知道当前在哪个活动?
让所有活动继承BaseActivity。
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //获取实例名 Log.d("baseActivity", getClass().getSimpleName()); //将当前实例添加 ActivityController.addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); ActivityController.removeActivity(this); } }
2.活动管理器 ActivityController
public class ActivityController { public static List<Activity> activities = new ArrayList<>(); /** * 添加活动 * * @param activity */ public static void addActivity(Activity activity) { activities.add(activity); } /** * 删除活动 * * @param activity */ public static void removeActivity(Activity activity) { activities.remove(activity); } /** * 结束应用 */ public static void finishAll() { for (Activity activity : activities) { if (!activity.isFinishing()) { activity.finish(); } } } }