转载自 www.aipiti.cn/read/4e26db…
在使用 ARound 开发多模块APP路由跳转时真的很是方便,ARound 拦截器(Interceptor)用于某些 Activity 用户登陆校验更是方便,面向切面声明简单实现方便。只要集成 IInterceptor 类,实现 public void process(Postcard postcard, InterceptorCallback callback) 方法,而后添加注解 @Interceptor(priority = 8, name = "测试用拦截器") 便可。ide
附带一个拦截器的声明:post
@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 秒。ui
Postcard 部分源代码this
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;spa
// Animation
private Bundle optionsCompat; // The transition animation of activity
private int enterAnim = -1;
private int exitAnim = -1;
....
复制代码
简单的解决办法进入拦截器的 process 方法时设置 postcard.setTimeout(2); 导航超时2秒或更短,就不会出现上面的问题了。code