添加依赖到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)