ARouter 拦截器Interceptor中postcard设置setTag的用处

ARouter的拦截器Interceptor在处理过程中,postcard.setTag的用处在判断登录状态的场景中显得关键。当interceptor.process()调用onContinue时,会继续执行后续拦截器,而如果调用onInterrupt则会终止执行并根据postcard的tag决定是否调用NavigationCallback的onInterrupt。源码分析揭示,如果有tag,将触发onInterrupt,否则会进行正常的activity跳转。这样通过postcard的tag实现拦截逻辑,确保未登录用户被正确拦截并引导至登录页面。
摘要由CSDN通过智能技术生成

在处理需求时,看到项目中某个功能用到了ARouter的拦截器来判断登录状态 ,没登录就会跳回登录页,但是interceptor的process()中发现异常情况:

            postcard.setTag("登录已失效,请重新登录");
            postcard.setExtra(666);
            callback.onContinue(postcard);

和正常流程:

callback.onContinue(postcard);

都是调的onContinue,一开始就很迷惑还以为代码出问题了,为啥onContinue还是会走到Navigationcallback的onInterrupt进行了拦截呢?我感觉应该会正常跳转走到onArrival了啊,使用ARouter的代码如下:

         ARouter.getInstance()
                .build("/message/live")
                .withString("roomId", roomId)
                .navigation(mContext, new NavCallback() {
                    @Override
                    public void onArrival(Postcard postcard) {
                    }

                    @Override
                    public void onInterrupt(Postcard postcard) {
                        jumpLoginPage(postcard);
                    }
                });

顺着源码走了一下,终于搞明白了:

    public Object navigation(Context context, NavigationCallback callback) {
        return ARouter.getInstance().navigation(context, this, -1, callback);
    }
protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        try {
            LogisticsCenter.completion(postcard);
        } catch (NoRouteFoundException ex) {
            logger.warning(Consts.TAG, ex.getMessage());

            if (debuggable()) { // Show friendly tips for user.
                Toast.makeText(mContext, "There's no route matched!\n" +
                        " Path = [" + postcard.getPath() + "]\n" +
                        " Group &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值