android 栈的优化 activity管理器

public class ActivityManager {
    private static List<Activity> activityList;
    private static ActivityManager instance;

    public static ActivityManager getInstance() {
        if (instance == null) instance = new ActivityManager();
        return instance;
    }

    /**
     * 添加activity
     */
    public static void addActivity(Activity activity) {
        if (activityList == null) {
            activityList = new ArrayList<>();
            if (activity != null) {
                activityList.add(activity);
            }
        }
    }

    /**
     * 获取 栈中activity 的size
     */

    public static int activitySize() {
        return activityList != null ? activityList.size() : 0;
    }

    /**
     * 获取 栈中最后的activity
     *
     * @return current Activity
     */
    public static Activity getCurrentActivity() {
        return activityList != null && activityList.size() > 0 ? activityList.get(activityList.size() - 1) : null;
    }

    /**
     * 查找指定的activity
     *
     * @param  cls
     */
    public static Activity getActivity(Class<?> cls) {
        if (activityList == null) return null;
        for (Activity activity : activityList) {
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }
    /**
     * 结束指定activity
     *
     * @param  activity
     */
    public static void removeActivity(Activity activity){
        if (activity == null){
            activityList.remove(activity);
        }
    }
    /**
     *销毁指定activity
     *
     * @param  cls
     */
    public static void destroyActivity(Class<?> cls){
      if (activityList == null) return;
      for (Activity activity : activityList){
          if (activity.getClass().equals(cls)){
              activity.finish();
          }
      }
    }
    /**
     *结束指定activity
     *
     * @param  cls
     */
    public static void removeActivity(Class<?> cls){
        if (activityList == null) return;
        for (Activity activity : activityList){
            if (activity.getClass().equals(cls)){
               activityList.remove(activity);
            }
        }
    }
    /**
     * 结束所有Activity
     */
    public static void finishAllActivity() {
        if (activityList == null) {
            return;
        }
        int size = activityList.size();
        for (int i = 0; i < size; i++) {
            if (null != activityList.get(i)) {
                activityList.get(i).finish();
            }
        }
        activityList.clear();
    }

    /**
     * 结束其他所有的Activity
     *
     * @param activity 不需要销毁的Activity
     */
    public static void finishOtherAllActivity(Activity activity) {
        if (activityList == null) {
            return;
        }
        List<Activity> removeList=new ArrayList<>();
        for (int i = 0, size = activityList.size(); i < size; i++) {
            if (activity != activityList.get(i)) {
                activityList.get(i).finish();
                removeList.add(activity);
            }
        }
        activityList.removeAll(removeList);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值