ARouter源码分析(1)-基本路由流程

94 篇文章 0 订阅
94 篇文章 0 订阅

文章是作者学习ARouter的源码的重点纪要。 ARouter官方文档 : https://github.com/alibaba/ARouter/blob/master/README_CN.md

先通过一张图来了解ARouter的路由过程以及相关类:

ARouter路由流程.png

本文先不对路由表的生成做详细了解,也不对InterceptorServiceGlobalDegradeService的加载做仔细分析,我们就先根据源码来大致看一遍路由的基本过程。

基本路由流程

ARouter中如果使用一个 @Route(path = "/test/activity1")注解标注在了一个Activity上,那么这个Activity就是可被路由的。我们可以通过调用下面代码实现页面跳转:

    ARouter.getInstance()
                .build("/test/activity1")
                .navigation(context, new NavigationCallback());

ARouter只是一个门面类,实际功能的实现是由_ARouter完成的。来看一下_ARouter.navigation()方法。注意为了只看主流程,我删去了一些逻辑

   protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        try {
            LogisticsCenter.completion(postcard); //装配 postcard
        } catch (NoRouteFoundException ex) {
            logger.warning(Consts.TAG, ex.getMessage());

            if (null != callback) {
                callback.onLost(postcard); //页面降级
            } else {   
                DegradeService degradeService = ARouter.getInstance().navigation(DegradeService.class);
                degradeService.onLost(context, postcard); //全局降级
            }
            return null;
        }

        if (null != callback) callback.onFound(postcard);

        //新开线程,异步执行拦截器
        interceptorService.doInterceptions(postcard, new InterceptorCallback() {
    
            @Override public void onContinue(Postcard postcard) {
                _navigation(context, postcard, requestCode, callback);
            }

            @Override public void onInterrupt(Throwable exception) {
                callback.onInterrupt(postcard);
            }
        });
        return null;
    }

LogisticsCenter 装配 Postcard

    public synchronized static void completion(Postcard postcard) {
        //从路由表中获取该路由的RouteMeta
        RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());

        if (null == routeMeta) {   //路由表中不存在就尝试加载
            Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup());  // 首先把这次路由对应的路由组的表加载出来
            if (null == groupMeta) {
                throw new NoRouteFoundException(TAG + "There is no route match the path [" + postcard.getPath() + "], in group [" + postcard.getGroup() + "]");
            } else {
                try {
                    IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
                    iGroupInstance.loadInto(Warehouse.routes);
                    Warehouse.groupsIndex.remove(postcard.getGroup());
                } catch (Exception e) {
                    throw new HandlerException(TAG + "Fatal exception when loading group meta. [" + e.getMessage() + "]");
                }
                completion(postcard);   // Reload
            }
        } else {
            //把对用的RouteMeta信息设置到Postcard中
            postcard.setDestination(routeMeta.getDestination()); // destination这里就是Activity类对象
            postcard.setType(routeMeta.getType());
            ....
            postcard.withString(ARouter.RAW_URI, postcard.getUri().toString());
            ...
        }
    }

来总结一下这个方法的主要流程:

  1. 尝试从路由表Warehouse.routes获取postcard.getPath()对应的路由信息。
  2. 如果不存在RouteMeta,则加载postcard.getPath()对应的路由组表Warehouse.groupsIndex。如果根本不存在对应的组,则路由失败
  3. 实例化Warehouse.groupsIndex, 然后把表内的信息加载到Warehouse.routes表中,再次调用completion()
  4. 获取到postcard.getPath()对应的RouteMeta,使用RouteMeta完善Postcard

我们先来看一眼如何把Warehouse.groupsIndex的信息加载到Warehouse.routes中:

这其实IRouteGroup接口的实例是动态生成的:

public class ARouter$$Group$$test implements IRouteGroup {
  @Override
  public void loadInto(Map<String, RouteMeta> atlas) {
    atlas.put("/test/activity1", RouteMeta.build(RouteType.ACTIVITY, Test1Activity.class, "/test/activity1", "test", -1, -2147483648));
    atlas.put("/test/activity2", RouteMeta.build(RouteType.ACTIVITY, Test2Activity.class, "/test/activity2", "test", -1, -2147483648));
  }
}

loadInto()实际上是把路由信息放入到Warehouse.routes中。

浅尝辄止,我们继续往下看,这里我们直接看拦截器实例:

拦截器

@Route(path = "/arouter/service/interceptor") 
public class InterceptorServiceImpl implements InterceptorService {

    @Override
    public void doInterceptions(final Postcard postcard, final InterceptorCallback callback) {
        if (null != Warehouse.interceptors && Warehouse.interceptors.size() > 0) {
            ..确保拦截器初始化完毕
            LogisticsCenter.executor.execute(new Runnable() {
                @Override
                public void run() {
                    //依次执行每一个拦截器
                    _excute(0, interceptorCounter, postcard);
                }
            });
        } else {
            callback.onContinue(postcard);
        }
    }
}

@Route(path = "/arouter/service/interceptor")标注,其实这个拦截器会在运行时动态加载。可以看到大致逻辑就是:

在异步线程中依次执行每一个拦截器。其实在这里可以看出ARouter的拦截器是全局的,即每一次路由如果不设置不被拦截的标志,则都会把拦截器走一遍。

在Postcard组装完毕和拦截器执行完毕后,就会调用_navigation()

private Object _navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        final Context currentContext = null == context ? mContext : context;
        switch (postcard.getType()) {
            case ACTIVITY:
                // intent中设置一些信息。

                // Navigation in main looper.
                runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(requestCode, currentContext, intent, postcard, callback);
                    }
                });
                break;
            case ...
        }
        return null;
    }

到这里,一次Activity的路由算是完成了。不过这其中我们还有很多细节没有仔细讨论:1

  1. Warehouse.routes路由表和Warehouse.groupsIndex路由组表是如何加载的
  2. InterceptorServiceDegradeService 是如何加载的
  3. ARouter是支持在运行时期获取不在同一个库的类实例的,比如Fragment,这是怎么实现的呢?
  4. ..

这些点,我们在后续文章再继续分析

 

 

+qq群:853967238。获取以上高清技术思维图,以及相关技术的免费视频学习资料。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值