ARouter
配置
implementation 'com.alibaba:arouter-api:1.5.0'
kapt 'com.alibaba:arouter-compiler:1.2.2'
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath "com.alibaba:arouter-register:1.0.2"
}
}
apply plugin: 'kotlin-kapt'
apply plugin: 'com.alibaba.arouter'
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName(), AROUTER_GENERATE_DOC: "enable"]
}
}
}
}
使用
if (isDebug()) { // 这两行必须写在init之前,否则这些配置在init过程中将无效
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
}
ARouter.init(mApplication); // 尽可能早,推荐在Application中初始化
ARouter.getInstance().build("/testactivity/Test1Activity")
.withString("name", "老王")
.withInt("age", 18)
.withBoolean("boy", true)
.withLong("high", 180)
.withString("url", "https://a.b.c")
.withSerializable("ser", testSerializable)
.withParcelable("pac", testParcelable)
.withObject("obj", testObj)
.withObject("objList", objList)
.withObject("map", map)
.navigation(this,500);
// .navigation();
// .navigation(this,new NavCallback(){
// @Override
// public void onArrival(Postcard postcard) {
//
// }
// });
@Route(path = "/testactivity/Test1Activity", name = "测试用 Activity")
public class Test1Activity extends AppCompatActivity {
@Autowired(desc = "姓名")
String name = "jack";
@Autowired(name = "boy", required = true)
boolean girl;
@Autowired
float fl = 12.00f;
@Autowired
TestSerializable ser;
@Autowired
TestParcelable pac;
@Autowired
TestObj obj;
@Autowired
List<TestObj> objList;
@Autowired
Map<String, List<TestObj>> map;
@Autowired
HelloService helloService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
ARouter.getInstance().inject(this);
helloService.sayHello("Hello moto.");
}
@Interceptor(priority = 6)
class Test1Interceptor:IInterceptor{
val TAG = Test1Interceptor::class.java.simpleName
private var mContext: Context? = null
override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
Log.d(TAG,"process:"+postcard?.path)
if(postcard?.path.equals("/testactivity/Test1Activity")) {
postcard?.withString("extra", "我是在拦截器中附加的参数")
callback?.onContinue(postcard)
}else {
callback?.onContinue(postcard)
}
}
override fun init(context: Context?) {
mContext = context
}
}
- 传递Object参数时,需要定义Json转换方法,方便使用不同的json处理工具
@Route(path = "/service/JsonServiceImpl")
class JsonServiceImpl:SerializationService{
override fun <T : Any?> json2Object(input: String?, clazz: Class<T>?): T {
return JSON.parseObject(input,clazz)
}
override fun init(context: Context?) {
}
override fun object2Json(instance: Any?): String {
return JSON.toJSONString(instance)
}
override fun <T : Any?> parseObject(input: String?, clazz: Type?): T {
return JSON.parseObject(input,clazz)
}
}
- Service,可以通过Arouter.getInstrance找到Service并做一些逻辑处理
@Route(path = "/service/SingleService")
class SingleService:IProvider{
internal var mContext: Context? = null
fun sayHello(name: String) {
Toast.makeText(mContext, "Hello $name", Toast.LENGTH_SHORT).show()
}
override fun init(context: Context) {
mContext = context
}
}
ARouter.getInstance().navigation(SingleService.class).sayHello("Mike");
//或者
((SingleService)(ARouter.getInstance().build("/service/SingleService").navigation())).sayHello("Mike");
- 跳转导航
可以用AndroidStudio的插件ARouter Helper
实现点击导航
网友评论