面试被问到过arouter看没看过源码 实际是怎么进行页面的跳转的 简单啃了一下源码 缕了一下跳转的流程 简单记录一下
1,用法
1)跳转用法
Postcard build = ARouter.getInstance().build(ActivityRouteUtils.ChooseIdentityActivity);
build.withInt("tag", id == R.id.sbtn_register ? 0 : 1).navigation();
2)标记activity标签
@Route(path = ActivityRouteUtils.SplashNewActivity)
2,解析
1)ARouter.getInstance().build(ActivityRouteUtils.ChooseIdentityActivity);解析
Build经过path的判断 非空等判断然后new一个postcard
下边是源码
protected Postcard build(String path, String group) {
if (TextUtils.isEmpty(path) || TextUtils.isEmpty(group)) {
throw new HandlerException(Consts.TAG + "Parameter is invalid!");
} else {
PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
if (null != pService) {
path = pService.forString(path);
}
return new Postcard(path, group);
}
}
2)Postcard extends RouteMeta
类分析 主要方法
里边有个Bundle 用来携带参数 activity/fragment之间的传值
WithString ,withInt等方法
Postcard(String path, String group, Uri uri, Bundle bundle
)里有{
RouteMeta.setPath(path)}
3) RouteMeta类分析
public RouteMeta(Route route, Element rawType, RouteType type, Map<String, Integer> paramsType) {
this(type, rawType, null, route.path(), route.group(), paramsType, route.priority(), route.extras());
}
有个Route注解 就是activity标记的 根据注解获取path等需要的参数
然后复制到path里
最后navigation()
调用return _ARouter.getInstance().navigation(mContext, postcard, requestCode, callback);.
4) _ARouter.getInstance().navigation(mContext, postcard, requestCode, callback);
方法解析
做了一些判断之后 调用_navigation
判断是
case ACTIVITY: 进行实际跳转
case PROVIDER:
return postcard.getProvider();
case BOARDCAST:
case CONTENT_PROVIDER:
case FRAGMENT:
5)@router注解源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface Route {
/**
* Path of route
*/
String path();
/**
* Used to merger routes, the group name MUST BE USE THE COMMON WORDS !!!
*/
String group() default "";
/**
* Name of route, used to generate javadoc.
*/
String name() default "undefined";
/**
* Extra data, can be set by user.
* Ps. U should use the integer num sign the switch, by bits. 10001010101010
*/
int extras() default Integer.MIN_VALUE;
/**
* The priority of route.
*/
int priority() default -1;
}