android 栈的优化 activity管理器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34272093/article/details/80838258
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);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34272093/article/details/80838258