package com.jingubank.main.common.app; import android.app.Activity; import android.content.Context; import android.content.Intent; import com.jingubank.main.finance.index.IndexActivity; import com.jingubank.main.utils.ActivityAnim; import java.util.Stack; public class AppManager { public static Stack<Activity> activityStack; private static AppManager instance; private long exitTime = 0; public AppManager() { } public static AppManager getAppManager() { if (instance == null) { instance = new AppManager(); } return instance; } // 退出栈顶Activity,即结束指定的Activity public void finishActivity(Activity activity) { if (activity != null) { activity.finish(); activityStack.remove(activity); activity = null; } } // 获得当前栈顶Activity public Activity currentActivity() { Activity activity = null; if (null != activityStack && 0 != activityStack.size()) { activity = activityStack.lastElement(); } return activity; } // 将当前Activity推入栈中 public void addActivity(Activity activity) { if (activityStack == null) { activityStack = new Stack<Activity>(); } activityStack.add(activity); } // 将当前Activity移出栈中 public void removeActivity(Activity activity) { if (activity != null) { activityStack.remove(activity); } } // 除了某个Activity,退出栈中其它所有Activity public void popAllActivityExceptOne(Class<?> cls) { if (null == activityStack || activityStack.size() == 0) { return; } int len = activityStack.size(); for (int i = len - 1; i >= 0; i--) { Activity activity = activityStack.get(i); if (activity == null || (null != cls && activity.getClass().equals(cls))) { continue; } finishActivity(activity); } } // 退出栈中当前Activity public void popUpActivity(Class<?> cls) { if (null == activityStack || activityStack.size() == 0) { return; } int len = activityStack.size(); for (int i = len - 1; i >= 0; i--) { Activity activity = activityStack.get(i); if (activity == null) continue; if (activity.getClass().equals(cls)) { finishActivity(activity); break; } } } /** * 结束所有activity */ public void finishAllActivities() { for (Activity activity : activityStack) { activity.finish(); } activityStack.clear(); } public void gotoMainActivity(Context context) { popAllActivityExceptOne(IndexActivity.class); Intent intent = new Intent(); intent.setAction("GO_BACK_MAIN_PAGE"); context.sendBroadcast(intent); }}
// ------------------调用方式--------------------------//
BaseActivity基类
onCreate()
AppManager.getAppManager().addActivity(this);//推入栈中
onDestory()@Override protected void onDestroy() { super.onDestroy(); AppManager.getAppManager().removeActivity(this); }
Activity堆栈管理及其使用
最新推荐文章于 2024-06-03 19:15:12 发布