-
Notifications
You must be signed in to change notification settings - Fork 386
3、如何使用ARouter
basiclib模块中增加以下依赖,basiclib是组件化框架中共用的依赖库:
compile 'com.alibaba:arouter-api:1.3.0'
在跳转的目标组件的build.gradle中,增加以下配置:
android {
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = [ moduleName : project.getName() ]
}
}
}
}
dependencies {
annotationProcessor 'com.alibaba:arouter-compiler:1.1.4'
...
}
在组件化框架中,我们的示例是从readercomponent跳转到sharecomponent,所以上述配置增加在sharecomponent下面的build.gradle中。
我们以“分享图书” 页面为例
@Route(path = "/share/shareBook")
public class ShareActivity extends AppCompatActivity {
在进入这个页面,需要传入两个参数,一个是String类型的bookName,一个是自定义类型Author的author
@Autowired
String bookName;
@Autowired
Author author;
由于自定义类型Author需要跨组件传递,我们知道,DDComponent的核心之处就是在组件之间见了一堵墙,在编译期代码和资源都是完全隔离的,所以Author必须定义在share组件向外提供的服务中。所以我们在component中,定义Author类:
public class Author {
private String name;
private int age;
private String county;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
}
现在就解决了Author的可见性问题,但是为了能在路由中传递,按照ARouter的要求,还需要自己实现SerializationService:
@Route(path = "/service/json")
public class JsonServiceImpl implements SerializationService {
@Override
public void init(Context context) {}
@Override
public <T> T json2Object(String text, Class<T> clazz) {
return JSON.parseObject(text, clazz);
}
@Override
public String object2Json(Object instance) {
return JSON.toJSONString(instance);
}
@Override
public <T> T parseObject(String input, Type clazz) {
return JSON.parseObject(input, clazz);
}
}
在组件化框架demo中,发起跳转是readercomponent中的ReaderFragment中,demo中列出了两个示例: 普通跳转
private void goToShareActivityNormal() {
Author author = new Author();
author.setName("Margaret Mitchell");
author.setCounty("USA");
ARouter.getInstance().build("/share/shareBook")
.withString("bookName", "Gone with the Wind")
.withObject("author", author)
.navigation();
}
以及startActivityForResult
private void goToShareActivityForResult() {
Author author = new Author();
author.setName("Margaret Mitchell");
author.setCounty("USA");
ARouter.getInstance().build("/share/shareMagazine")
.withString("bookName", "Gone with the Wind")
.withObject("author", author)
.navigation(getActivity(), REQUEST_CODE);
}
经过上面的操作,已经可以完成UI跳转了。但是如果运行demo就可以发现,此时即使卸载了分享组件,分享书的页面还是可以进入的,说明生命周期没有同步。在DDComponent自带的方案中是不存在这个问题的,因为跳转的逻辑已经与组件化生命周期绑定在一起。 这里就用到ARouter自带的拦截器功能,每个组件都需要定义一个拦截器,当组件卸载之后需要拦截住该组件的跳转入口。 下面是分享组件拦截器的示例代码:
@Interceptor(priority = 1, name = "分享组件拦截器")
public class ShareInterceptor implements IInterceptor {
public static boolean isRegister;
Context mContext;
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
if (isRegister) {
callback.onContinue(postcard);
} else if ("/share/shareBook".equals(postcard.getPath())
|| "/share/shareMagazine".equals(postcard.getPath())) {
MainLooper.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, "分享组件已经卸载", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void init(Context context) {
mContext = context;
}
}
这里通过一个isRegister开关来控制拦截器是否生效,为了保证生命周期一致性,在ShareApplike中增加赋值逻辑:
public class ShareApplike implements IApplicationLike {
@Override
public void onCreate() {
ShareInterceptor.isRegister = true;
}
@Override
public void onStop() {
ShareInterceptor.isRegister = false;
}
}