轉載自 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