Activity栈管理

下面是我从其他地方搞来的Activity栈管理类,实现的是弱引用

public class ActivityStackManager {
    private static final String TAG = "ActivityStackManager";
    /**
     * Activity栈
     */
    private Stack> mActivityStack;
    private static ActivityStackManager activityStackManager = new ActivityStackManager();
    private ActivityStackManager() {
    }
    /***
     * 获得AppManager的实例
     *
     * @return AppManager实例
     */
    public static ActivityStackManager getInstance() {
        if (activityStackManager == null) {
            activityStackManager = new ActivityStackManager();
        }
        return activityStackManager;
    }
    /***
     * 栈中Activity的数
     *
     * @return Activity的数
     */
    public int stackSize() {
        return mActivityStack.size();
    }
    /***
     * 获得Activity栈
     *
     * @return Activity栈
     */
    public Stack> getStack() {
        return mActivityStack;
    }
    /**
     * 添加Activity到堆栈
     */
    public void addActivity(WeakReference activity) {
        if (mActivityStack == null) {
            mActivityStack = new Stack<>();
        }
        mActivityStack.add(activity);
    }
    /**
     * 删除ac
     *
     * @param activity 弱引用的ac
     */
    public void removeActivity(WeakReference activity) {
        if (mActivityStack != null) {
            mActivityStack.remove(activity);
        }
    }
    /***
     * 获取栈顶Activity(堆栈中最后一个压入的)
     *
     * @return Activity
     */
    public Activity getTopActivity() {
        Activity activity = mActivityStack.lastElement().get();
        if (null == activity) {
            return null;
        } else {
            return mActivityStack.lastElement().get();
        }
    }
    /***
     * 通过class 获取栈顶Activity
     *
     * @param cls
     * @return Activity
     */
    public Activity getActivityByClass(Class cls) {
        Activity return_activity = null;
        for (WeakReference activity : mActivityStack) {
            if (activity.get().getClass().equals(cls)) {
                return_activity = activity.get();
                break;
            }
        }
        return return_activity;
    }
    /**
     * 结束栈顶Activity(堆栈中最后一个压入的)
     */
    public void killTopActivity() {
        try {
            WeakReference activity = mActivityStack.lastElement();
            killActivity(activity);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
    /***
     * 结束指定的Activity
     *
     * @param activity
     */
    public void killActivity(WeakReference activity) {
        try {
            Iterator> iterator = mActivityStack.iterator();
            while (iterator.hasNext()) {
                WeakReference stackActivity = iterator.next();
                if (stackActivity.get() == null) {
                    iterator.remove();
                    continue;
                }
                if (stackActivity.get().getClass().getName().equals(activity.get().getClass().getName())) {
                    iterator.remove();
                    stackActivity.get().finish();
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
    /***
     * 结束指定类名的Activity
     *
     * @param cls
     */
    public void killActivity(Class cls) {
        try {
            ListIterator> listIterator = mActivityStack.listIterator();
            while (listIterator.hasNext()) {
                Activity activity = listIterator.next().get();
                if (activity == null) {
                    listIterator.remove();
                    continue;
                }
                if (activity.getClass() == cls) {
                    listIterator.remove();
                    if (activity != null) {
                        activity.finish();
                    }
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
    /**
     * 结束所有Activity
     */
    public void killAllActivity() {
        try {
            ListIterator> listIterator = mActivityStack.listIterator();
            while (listIterator.hasNext()) {
                Activity activity = listIterator.next().get();
                if (activity != null) {
                    activity.finish();
                }
                listIterator.remove();
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
    /**
     * 移除除了某个activity的其他所有activity
     *
     * @param cls 界面
     */
    public void killAllActivityExceptOne(Class cls) {
        try {
            for (int i = 0; i < mActivityStack.size(); i++) {
                WeakReference activity = mActivityStack.get(i);
                if (activity.getClass().equals(cls)) {
                    break;
                }
                if (mActivityStack.get(i) != null) {
                    killActivity(activity);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        killAllActivity();
        Process.killProcess(Process.myPid());
    }
}

你可能感兴趣的:(Activity管理)