美文网首页Androidandroid重构项目Rxjava
android 手动管理activity栈,实现跳转指定页面

android 手动管理activity栈,实现跳转指定页面

作者: 风___________ | 来源:发表于2017-06-27 19:48 被阅读272次

    需求:某层次很深的一个acticity有个退出登录按钮
    难点:for循环finish太不优雅了
    实现:

    第一步: 工具类

    import android.app.Activity;
    
    import java.util.Stack;
    
    /**
     * Created by jing on 17/5/24.
     */
    // 管理 Activity 的 queue
    // Application 使用
    public class KapApplicationActivitysQueue {
        private KapApplicationActivitysQueue(){}
        private static KapApplicationActivitysQueue queue = new KapApplicationActivitysQueue();
        public static KapApplicationActivitysQueue ShareActivityQueue(){
            return queue;
        }
        private Stack<Activity> activityStack = new Stack<Activity>();
        /**
         * addActivity 添加Activity到堆栈
         * popCurrentActivity 结束当前Activity
         * 只有这俩方法 操作 activityStack 不能手动调用(都是自动添加删除的)
         * currentActivity 获取当前的activity,不做任何操作
         */
        public void addActivity(Activity activity) {
            activityStack.push(activity);
        }
        public  void popCurrentActivity() {
            activityStack.pop();
        }
        public  Activity currentActivity() {
            return activityStack.lastElement();
        }
    
    
        /**
         * 辅助方法
         * 下面的这些方法都是,注意防止当前activity结束当前的导致crash
         * 获取当前Activity(堆栈中最后一个压入的)
         */
        /**
         * finishOneActivity 结束指定的Activity
         * activity
         * Class<?> cls  activity的class
         */
        public  void finishOneActivity(Activity activity) {
            if (activity != null) {
                if(!activity.isFinishing()) {
                    activity.finish();
                }
            }
        }
        public  void finishOneActivity(Class<?> cls) {
            for (Activity activity : activityStack) {
                if (!activity.getClass().equals(cls)) continue;
                finishOneActivity(activity);
                return;
            }
        }
    
        /**
         * 结束除指定类名的所有Activity
         */
        public  void finishExcludeActivityAllActivity(Class<?> cls) {
            for (Activity activity : activityStack) {
                if (activity == null) continue;
                if (activity.getClass().equals(cls)) continue;
                finishOneActivity(activity);
            }
        }
        /**
         * 结束所有Activity
         */
        public  void finishAllActivity() {
            for (Activity activity : activityStack) {
                if (activity == null) continue;
                finishOneActivity(activity);
            }
        }
    //    /**
    //     * 退出应用程序
    //     */
    //    public static void AppExit(Context context) {
    //        try {
    //            finishAllActivity();
    //            ActivityManager manager = (ActivityManager) context
    //                    .getSystemService(Context.ACTIVITY_SERVICE);
    //            manager.killBackgroundProcesses(context.getPackageName());
    //            System.exit(0);
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }
    //    }
    }
    

    第二步:自定义的Application里面

    public class KapApplication extends Application{
    /**
         * activity栈管理
         * */
        private KapApplicationActivitysQueue activitysQueue = KapApplicationActivitysQueue.ShareActivityQueue();
        private ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                activitysQueue.addActivity(activity);//创建
            }
            @Override
            public void onActivityDestroyed(Activity activity) {
                activitysQueue.popCurrentActivity();// 推出
            }
    
            @Override
            public void onActivityStarted(Activity activity) {
            }
    
            @Override
            public void onActivityResumed(Activity activity) {
            }
    
            @Override
            public void onActivityPaused(Activity activity) {
            }
    
            @Override
            public void onActivityStopped(Activity activity) {
    
            }
    
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    
            }
        };
    // 下面是俩辅助方法,仅供参考
        /**
         * currentActivity 获取当前activity
         * logInActivityChangeAction 切换根视图登录
         * homeActivityChangeAction 切换根视图主页
         * */
        public static Activity currentActivity(){
            KapApplicationActivitysQueue activitysQueue = getInstance().activitysQueue;
            return activitysQueue.currentActivity();
        }
        public static void logInActivityChangeAction(){
            Class openClass = KapLoginActivity.class;
            changeRootActivityByClass(openClass);
        }
        public static void homeActivityChangeAction(){
            Class openClass = KapHomePageActivity.class;
            changeRootActivityByClass(openClass);
        }
        private static void changeRootActivityByClass(Class openClass){
            KapApplicationActivitysQueue activitysQueue = getInstance().activitysQueue;
            Activity currentActivity = activitysQueue.currentActivity();
            currentActivity.startActivity(new Intent(currentActivity, openClass));
            activitysQueue.finishExcludeActivityAllActivity(openClass);
        }
    }
    

    使用:

    private void pushActivity(){
                KapApplication.logInActivityChangeAction();//登录页
                KapApplication.homeActivityChangeAction();//主页
        }
    

    至此就完事了,更多特定的操作,看你们喜好了~~~

    相关文章

      网友评论

        本文标题:android 手动管理activity栈,实现跳转指定页面

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