ARouter拦截器多次点击bug

2,256 阅读1分钟

转载自 www.aipiti.cn/read/4e26db…
在使用 ARound 开发多模块APP路由跳转时真的非常方便,ARound 拦截器(Interceptor)用于某些 Activity 用户登录校验更是方便,面向切面声明简单实现方便。只要集成 IInterceptor 类,实现 public void process(Postcard postcard, InterceptorCallback callback) 方法,然后添加注解 @Interceptor(priority = 8, name = "测试用拦截器") 即可。

附带一个拦截器的声明:

@Interceptor(priority = 8, name = "用户验证") public class RouteInterceptor implements IInterceptor { private Context context;

@Override
public void process(Postcard postcard, InterceptorCallback callback) {
    //判断是否需要验证登录
    if(postcard.getExtra() == 2001){
        //读取配置文件中的登录状态
        boolean isLogin = GlobalData.getInstance().isLogin();
        //判断是否登录
        if(isLogin) {
            callback.onContinue(postcard);
        }else{
            ARouter.getInstance().build(ARouterCons.LOGIN_PATH).navigation();
        }
    }else{
        callback.onContinue(postcard);
    }
}

@Override
public void init(Context context) {
    this.context = context;
}

} 在使用 ARouter 拦截器时测试人员发现一个问,点击打开一个需要校验用户登录状态的 Activity 时,当用户没有登录重复的点击返回再点击,大概10次左右 ARouter 路由失效(所有路由进入不了拦截器)。这个问题整了半天才发现拦截器出现问题导致再次进入拦截器的时间延时了300秒,Postcard 类中有定义:private int timeout = 300; 导航超时 300 秒。

Postcard 部分源代码

public final class Postcard extends RouteMeta { // Base private Uri uri; private Object tag; // A tag prepare for some thing wrong. private Bundle mBundle; // Data to transform private int flags = -1; // Flags of route private int timeout = 300; // Navigation timeout, TimeUnit.Second private IProvider provider; // It will be set value, if this postcard was provider. private boolean greenChannel; private SerializationService serializationService;

// Animation
private Bundle optionsCompat;    // The transition animation of activity
private int enterAnim = -1;
private int exitAnim = -1;
....

简单的解决办法进入拦截器的 process 方法时设置 postcard.setTimeout(2); 导航超时2秒或更短,就不会出现上面的问题了。