清空所有Activity

退出APP是需要清空所有Activity, 清空的前提是 需要记录已开启的所有 Activity。
在Application.ActivityLifecycleCallbacks的生命周期中调用add和remove操作。

public class ActivityStackHelper {
    private static LinkedHashMap<Class<?>, Activity> activities = new LinkedHashMap<>();

    /**
     * 保存activity到集合中
     *
     * @param activity
     */
    public static void add(Activity activity) {
        if (activity == null) {
            return;
        }
        activities.put(activity.getClass(), activity);
    }

    /**
     * 判断指定的activity在map中是否存在
     *
     * @param clazz 指定的activity
     */
    public static boolean isActivityExist(Class clazz) {
        return clazz != null && get(clazz) != null;
    }

    /**
     * 获取指定clazz的activity
     *
     * @param clazz
     * @param 
     * @return
     */
    public static <T extends Activity> T get(Class<T> clazz) {
        return (T) activities.get(clazz);
    }

    /**
     * 获取当前集合中的activity
     *
     * @return
     */
    public static int getSize() {
        return activities.size();
    }

    /**
     * 指定activity是否是activity栈中最底层的activity
     *
     * @param activity
     * @return true,是;false,否
     */
    public static boolean isLastActivity(Activity activity) {
        if (activity == null) {
            return false;
        }
        if (activities == null || activities.size() == 0) {
            return false;
        }

        ListIterator<Map.Entry<Class<?>, Activity>> iterator =
                new ArrayList<>(activities.entrySet()).listIterator(activities.size());

        while (iterator.hasPrevious()) {
            Map.Entry<Class<?>, Activity> next = iterator.previous();
            if (next.getKey() == activity.getClass()) {
                return !iterator.hasPrevious();
            }
        }

        return false;
    }

    /**
     * 移除集合中的activity
     *
     * @param activity
     */
    public static void remove(Activity activity) {
        if (activity == null) {
            return;
        }
        activities.remove(activity.getClass());
    }

    /**
     * 清空并finish所有activity
     */
    public static void clearAndFinishActivity() {
        Set<Map.Entry<Class<?>, Activity>> entries = activities.entrySet();
        for (Map.Entry<Class<?>, Activity> entry : entries) {
            Activity activity = entry.getValue();
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        activities.clear();
    }

    /**
     * 清空并finish所有activity(除指定activity之外)
     *
     * @param clazzs
     */
    public static void clearAndFinishOtherActivity(Class... clazzs) {

        List<Class> cls = clazzs != null ? Arrays.asList(clazzs) : null;
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Class<?>, Activity> next = iterator.next();
            Class<?> key = next.getKey();
            if (cls != null && cls.contains(key)) {
                continue;
            }
            Activity value = next.getValue();
            if (!value.isFinishing()) {
                value.finish();
            }
            iterator.remove();
        }
    }

    /**
     * 指定activity是否在运行
     *
     * @param clazz
     * @param 
     * @return
     */
    public static <T extends Activity> boolean isActivityRunning(Class<T> clazz) {
        T activity = get(clazz);
        if (activity == null || activity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取当前栈顶activity
     *
     * @return
     */
    public static Activity getTopActivity() {
        if (activities == null || activities.size() == 0) {
            return null;
        }
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        Map.Entry<Class<?>, Activity> next = null;
        while (iterator.hasNext()) {
            next = iterator.next();
        }
        return next != null ? next.getValue() : null;
    }


    /**
     * 获取次栈顶activity
     *
     * @return
     */
    public static Activity getTopSecondActivity() {
        if (activities == null || activities.size() == 0) {
            return null;
        }
        int size = activities.size();
        if (size < 2){
            return null;
        }
        int index = 1;
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        Map.Entry<Class<?>, Activity> next = null;
        while (iterator.hasNext()) {
            next = iterator.next();
            if (index == size - 1){
                if (next != null){
                    LogUtils.i("ActivityStackHelper", "secondActivity" + next.getValue());
                    return next.getValue();
                }
            }
            index++;
        }
        return null;
    }

    /**
     * 获取除了指定activity之外的栈顶activity
     *
     * @param clazz
     * @return
     */
    public static Activity getTopActivityExclude(Class clazz) {
        if (activities == null || activities.size() == 0) {
            return null;
        }
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        Map.Entry<Class<?>, Activity> resule = null;
        while (iterator.hasNext()) {
            Map.Entry<Class<?>, Activity> next = iterator.next();
            if (next.getKey() != clazz) {
                resule = next;
            }
        }
        return resule != null ? resule.getValue() : null;
    }

}

调用使用方法

public class AppActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        ActivityStackHelper.add(activity);
    }
    @Override
    public void onActivityDestroyed(Activity activity) {
        ActivityStackHelper.remove(activity);
    }
  }

上述工具类用到了LinkedHashMap 是为了保证顺序,其特性详见 https://blog.csdn.net/iblade/article/details/108369770

你可能感兴趣的:(Android)