Activity 堆栈相关处理方法

作者: Demo_Yang | 来源:发表于2017-03-16 16:49 被阅读27次
      private static Stack<Activity> mActivityStack;
      
      /**
         * 添加Activity到堆栈
         */
        public void addActivity(Activity activity) {
            if (mActivityStack == null) {
                mActivityStack = new Stack<Activity>();
            }
            mActivityStack.add(activity);
        }
    
        /**
         * 获取栈顶Activity(堆栈中最后一个压入的)
         */
        public Activity getTopActivity() {
            Activity activity = mActivityStack.lastElement();
            return activity;
        }
    
        /**
         * 结束栈顶Activity(堆栈中最后一个压入的)
         */
        public void killTopActivity() {
            Activity activity = mActivityStack.lastElement();
            killActivity(activity);
        }
    
        /**
         * 结束指定的Activity
         */
        public void killActivity(Activity activity) {
            if (activity != null) {
                mActivityStack.remove(activity);
                activity.finish();
                activity = null;
            }
        }
    
        /**
         * 结束指定类名的Activity
         */
        public void killActivity(Class<?> cls) {
            for (Activity activity : mActivityStack) {
                if (activity.getClass().equals(cls)) {
                    killActivity(activity);
                }
            }
        }
    
        /**
         * 结束所有Activity
         */
        public void killAllActivity() {
            if (Tools.isEmpty(mActivityStack)) {
                return;
            }
            for (int i = 0, size = mActivityStack.size(); i < size; i++) {
                if (null != mActivityStack.get(i)) {
                    mActivityStack.get(i).finish();
                }
            }
            mActivityStack.clear();
        }
    
    

    相关文章

      网友评论

        本文标题:Activity 堆栈相关处理方法

        本文链接:https://www.haomeiwen.com/subject/zgyjnttx.html