实际开发中管理actitvy 创建管理栈 public class ActivityManager { private static Stack<BaseActivity> activityStack = new Stack<>(); /** * 添加Activity到堆栈 */ public static void addActivity(BaseActivity activity) { activityStack.push(activity); } /** * 获取当前Activity(堆栈中最后一个压入的) */ public static BaseActivity currentActivity() { return activityStack.lastElement(); } /** * 结束当前Activity(堆栈中最后一个压入的) */ public static void finishCurrentActivity() { BaseActivity activity = activityStack.pop(); activity.finish(); } /** * 结束指定的Activity */ public static void finishActivity(BaseActivity activity) { if (activity != null) { activityStack.remove(activity); if (!activity.isFinishing()) { activity.finish(); } } } /** * 结束指定类名的Activity */ public static void finishActivity(Class<?> cls) { for (BaseActivity activity : activityStack) { if (activity.getClass().equals(cls)) { finishActivity(activity); } } } /** * 结束所有Activity */ public static void finishAllActivity() { for (BaseActivity activity : activityStack) { if (activity != null) { activity.finish(); } } activityStack.clear(); } public static void finishAllExcept(BaseActivity keepActivity) { for (BaseActivity activity : activityStack) { if (activity != null && !activity.getClass().equals(keepActivity)) { activity.finish(); activityStack.remove(activity); } } } /** * 退出应用程序 */ public static void exitApp() { try { finishAllActivity(); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } }
实际开发中Activity管理
最新推荐文章于 2024-08-08 14:03:32 发布