Android路由框架-ARouter使用以及遇到的问题

一、页面路由基本介绍

1.什么是页面路由

  映射页面跳转关系,包含跳转相关的URL跳转及值传递、拦截器等功能。

2.为什么要使用页面路由

  在原始android开发中,当我们需要进行页面跳转时,正常写法如下:

Intent intent = new Intent(mContext, XXActivity.class);
intent.putExtra("key","value"); startActivity(intent); Intent intent = new Intent(mContext, XXActivity.class); intent.putExtra("key","value"); startActivityForResult(intent, 100); 

上述写法容易出现以下问题:

  1. 多人协同开发的时候,大家都去AndroidManifest.xml中定义各种IntentFilter,使用隐式Intent,最终发现AndroidManifest.xml中充斥着各种Schame,各种Path,需要经常解决Path重叠覆盖、过多的Activity被导出,引发安全风险等问题
  2. 跳转过程中无法插手:直接通过Intent的方式跳转,跳转过程开发者无法干预,一些面向切面的事情难以实施,比方说登录、埋点这种非常通用的逻辑,在每个子页面中判断又很不合理,毕竟activity已经实例化了
  3. 跨模块无法显式依赖:在App小有规模的时候,我们会对App做水平拆分,按照业务拆分成多个子模块,之间完全解耦,通过打包流程控制App功能,这样方便应对大团队多人协作,互相逻辑不干扰,这时候只能依赖隐式Intent跳转,书写麻烦,成功与否难以控制

    二、页面路由框架ARouter介绍

    1.常用功能介绍

    应用内页面跳转

    添加依赖

    温馨提示:api 的版本和 compiler 的版本号需要用最新的。最新的版本在 Github上可以找到。

    重写Application并初始化ARouter   配置将要跳转的页面

  4. 进行简单的页面跳转

  5. 温馨提示:支持数据类型如下:
    
    //基础类型
    .withString( String key, String value )
    .withBoolean( String key, boolean value)
    .withChar( String key, char value )
    .withShort( String key, short value)
    .withInt( String key, int value)
    .withLong( String key, long value)
    .withDouble( String key, double value)
    .withByte( String key, byte value)
    .withFloat( String key, float value)
    .withCharSequence( String key,  CharSequence value)
    
    //数组类型
    .withParcelableArrayList( String key, ArrayList<? extends Parcelable > value)
    .withStringArrayList( String key,  ArrayList<String> value)
    .withIntegerArrayList( String key, ArrayList<Integer> value)
    .withSparseParcelableArray( String key, SparseArray<? extends Parcelable> value)
    .withCharSequenceArrayList( String key, ArrayList<CharSequence> value)
    .withShortArray( String key,  short[] value)
    .withCharArray( String key, char[] value)
    .withFloatArray( String key, float[] value)
    .withCharSequenceArray( String key,  CharSequence[] value)
    
    //Bundle 类型
    .with( Bundle bundle )
    
    //Activity 跳转动画
    .withTransition(int enterAnim, int exitAnim)
    
    //其他类型
    .withParcelable( String key, Parcelable value)
    .withParcelableArray( String key,  Parcelable[] value)
    .withSerializable( String key, Serializable value)
    .withByteArray( String key, byte[] value)
    .withTransition(int enterAnim, int exitAnim)
    

    三:遇到的问题

    • ARouter.getInstance().inject(this);
      我们有一个singletask启动模式的activity,在onNewIntent方法中调用ARouter.getInstance().inject(this);得不到参数,查看ARouter在build过程中生成的代码可以知道它是调用了activity的getIntent来获取参数的,但是onNewIntent中的intent和在onCreate方法中的intent并不相同,所以需要在onNewIntent方法中调用setIntent方法,然后就能得到参数了。

      ARouter::Compiler >>> No module name, for more information, look at gradle log.
      检查项目依赖的全部module包括module依赖的module(没有页面的module也算),在每个module的 build.gradle中加上下面的代码。

    defaultConfig {
        
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [ AROUTER_MODULE_NAME : project.getName() ]
                }
            }
    }
    • ARouter there's no route matched

    不同module的一级路径必须不同,否则会导致一个moudle中的一级路径失效

    下面是抛出异常的源代码

        public synchronized static void completion(Postcard postcard) {
        if (null == postcard) {
            throw new NoRouteFoundException(TAG + "No postcard!");
        }
    
        //查找RouteMeta对象,如果存在说明路由成功,如果失败说明还没有被加载或者这个path就是错误的
        RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());
        if (null == routeMeta) {
          // 通过groupsIndex去找IRouteGroup的实现类
            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 {
                // 通过反射获取IRouteGroup的实现类,然后加载到内存
                IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
                iGroupInstance.loadInto(Warehouse.routes);
                // 加载到内存后Warehouse.groupsIndex去掉这个group
                Warehouse.groupsIndex.remove(postcard.getGroup());
            }
        } else {
            //查找到路由地址
            。。。。。
        }
    }
    by:yzl

猜你喜欢

转载自www.cnblogs.com/widgetbox/p/yzl.html