关于阿里推出的路由框架ARouter源码解析,整理了3家面试问题:美团+字节+腾讯

本文详细分析了阿里ARouter路由框架的源码,包括Postcard的构建、路径补全、服务获取以及拦截器机制。在Postcard构建过程中,涉及到PathReplaceService的预处理,通过extractGroup方法提取Route Group。导航流程中,重点讲解了navigation方法,处理不同类型的跳转,如Activity、Provider、Broadcast等,并介绍了如何通过interceptorService执行拦截器。Service的获取通过navigation(Class service)实现,兼容不同版本的service获取方式。最后,文章探讨了ARouter的拦截器处理流程,包括IInterceptor接口和CancelableCountDownLatch的使用,确保拦截器的正确执行。
摘要由CSDN通过智能技术生成

}

它转调到了 _ARouter 的 build 方法:

protected Postcard build(String path) {

if (TextUtils.isEmpty(path)) {

throw new HandlerException(Consts.TAG + “Parameter is invalid!”);

} else {

PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);

if (null != pService) {

path = pService.forString(path);

}

return build(path, extractGroup(path));

}

}

它首先通过 ARouter.navigation 获取到了 PathReplaceService,它需要用户进行实现,若没有实现会返回 null,若有实现则调用了它的 forString 方法传入了用户的 Route Path 进行路径的预处理。

最后转调到了 build(path, group),group 通过 extractGroup 得到:

private String extractGroup(String path) {

if (TextUtils.isEmpty(path) || !path.startsWith("/")) {

throw new HandlerException(Consts.TAG + “Extract the default group failed, the path must be start with ‘/’ and contain more than 2 ‘/’!”);

}

try {

String defaultGroup = path.substring(1, path.in
dexOf("/", 1));

if (TextUtils.isEmpty(defaultGroup)) {

throw new HandlerException(Consts.TAG + “Extract the default group failed! There’s nothing between 2 ‘/’!”);

} else {

return defaultGroup;

}

} catch (Exception e) {

logger.warning(Consts.TAG, "Failed to extract default group! " + e.getMessage());

return null;

}

}

extractGroup 实际上就是对字符串处理,取出 Route Group 的名称部分。

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);

}

}

build(path, group) 方法同样也会尝试获取到 PathReplaceService 并对 path 进行预处理。之后通过 path 与 group 构建了一个 Postcard 类:

public Postcard(String path, String group) {

this(path, group, null, null);

}

public Postcard(String path, String group, Uri uri, Bundle bundle) {

setPath(path);

setGroup(group);

setUri(uri);

this.mBundle = (null == bundle ? new Bundle() : bundle);

}

这里最终调用到了 PostCard(path, group, uri, bundle),这里只是进行了一些参数的设置。

之后,如果我们调用 withIntwithDouble 等方法,就可以进行参数的设置。例如withInt方法:

public Postcard withInt(@Nullable String key, int value) {

mBundle.putInt(key, value);

return this;

}

它实际上就是在对 Bundle 中设置对应的 key、value。

最后我们通过 navigation 即可实现最后的跳转:

public Object navigation() {

return navigation(null);

}

public Object navigation(Context context) {

return navigation(context, null);

}

public Object navigation(Context context, NavigationCallback callback) {

return ARouter.getInstance().navigation(context, this, -1, callback);

}

public void navigation(Activity mContext, int requestCode) {

navigation(mContext, requestCode, null);

}

public void navigation(Activity mContext, int requestCode, NavigationCallback callback) {

ARouter.getInstance().navigation(mContext, this, requestCode, callback);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值