阿里 ARouter的使用和注意点

添加依赖到baselib的build gradle中:

ext.arouter_api_version = '1.2.2'
//ARouter
    api "com.alibaba:arouter-api:$arouter_api_version"

在使用的每个lib module中添加:

   ext.arouter_compiler_version = '1.1.3'

  apply plugin: 'kotlin-kapt'
 //ARouter
    kapt "com.alibaba:arouter-compiler:$arouter_compiler_version"

在application中添加初始化:

//ARouter初始化
        ARouter.openLog()    // 打印日志
        ARouter.openDebug()
        ARouter.init(this)

开始使用:在build方法中指定跳转路由 注意:路由路径不可以重复,例如:
“/test/1"和”/test/2",test位置是不可以重复使用的

// 1. Simple jump within application (Jump via URL in 'Advanced usage')
ARouter.getInstance().build("/test/activity").navigation();

// 2. Jump with parameters
ARouter.getInstance().build("/test/activity")
            .withLong("key1", 666L)
            .withString("key3", "888")
            .withObject("key4", new Test("Jack", "Rose"))
            .navigation();

接收路由:

// Declare a field for each parameter and annotate it with @Autowired
@Route(path = "/test/activity")
public class Test1Activity extends Activity {
    @Autowired
    public String name;
    @Autowired
    int age;
    @Autowired(name = "girl") // Map different parameters in the URL by name
    boolean boy;
    @Autowired
    TestObj obj;    // Support for parsing custom objects, using json pass in URL

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

        // ARouter will automatically set value of fields
        Log.d("param", name + age + boy);
    }
}

注意点:
在这里插入图片描述
索要天转的activity需要在oncreate中添加:

ARouter.getInstance().inject(this);

注意:传递的参数也可以通过intent进行接收

跨模块数据传递:

在baseLib中创建一个接口实现IProvider接口:

import com.alibaba.android.arouter.facade.template.IProvider

/*
    跨模块接口调用 接口定义
 */
interface PushProvider:IProvider {
    fun getPushId():String//自己定义的扩展方法
}

是使用的A模块中实现PushProvide接口并对接口进行扩展

/*
    模块间接口调用
    提供PushId的实现
 */
@Route(path = RouterPath.MessageCenter.PATH_MESSAGE_PUSH)
class PushProviderImpl:PushProvider {

    private var mContext:Context? = null
    override fun getPushId(): String {
        return JPushInterface.getRegistrationID(mContext)
    }

    override fun init(context: Context?) {
        mContext = context
    }
}

在其他B模块中获取兄弟A模块中的数据:

@Autowired(name = RouterPath.MessageCenter.PATH_MESSAGE_PUSH)
    @JvmField
    var mPushProvider:PushProvider? = null
    //使用提供的数据
    mPushProvider?.getPushId()?:""
    //不要忘记在使用的页面中添加:
    //ARouter注册
        ARouter.getInstance().inject(this)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值