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. 1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    温馨提示:支持数据类型如下:

     

    //基础类型

    .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 {
            //查找到路由地址
            。。。。。
        }
    }

    转载于:https://www.cnblogs.com/widgetbox/p/yzl.html

     

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android ARouter 是一个用于实现组件、模块开发的路由框架,它提供了一种简单的方式来实现不同组件之间的跳转和通信。 使用 ARouter 的步骤如下: 1. 在项目的 build.gradle 文件中添加依赖: ```groovy implementation 'com.alibaba:arouter-api:x.x.x' annotationProcessor 'com.alibaba:arouter-compiler:x.x.x' ``` 请将 `x.x.x` 替换为最新的版本号。 2. 在需要使用 ARouter 的模块中,创建一个类似于 Application 的类,并在其 `onCreate()` 方法中进行 ARouter 的初始: ```java public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); if (BuildConfig.DEBUG) { ARouter.openLog(); ARouter.openDebug(); } ARouter.init(this); } } ``` 3. 在需要跳转到的目标页面中,使用 `@Route` 注解进行标记,以便 ARouter 进行识别: ```java @Route(path = "/path/to/target") public class TargetActivity extends AppCompatActivity { // ... } ``` 4. 在需要跳转的地方,通过调用 `ARouter.getInstance().build("/path/to/target").navigation()` 来实现页面跳转: ```java ARouter.getInstance().build("/path/to/target").navigation(); ``` 5. 如果需要传递参数,可以使用 `withXxx` 方法来添加参数: ```java ARouter.getInstance() .build("/path/to/target") .withString("key", "value") .navigation(); ``` 通过以上步骤,你就可以在 Android 项目中使用 ARouter 进行页面跳转和参数传递了。当然,ARouter 还提供了其他功能,比如拦截器、URI 跳转等,你可以根据具体需求进行使用。希望对你有所帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值