前文:希望可以坚持写下去…
ARouter是阿里2017年开源的页面路由框架,是目前比较火的一个开源框架。官方对这个框架的定义是ARouter是阿里巴巴开源的Android平台中对页面、服务提供路由功能的中间件,提倡的是简单且够用。
1:原生方法就是通过Intent进行实现。两种方式:显示意图,直接将目标页面放入intent中,特点就是粗暴,简单,劣势就是,目标页面变更,需要逐个修改,不好管理;隐示意图,通过配置目标标记,并在清单文件配置后,通过该字符串调用,特点就是,目标页面变更,便于全局修改,劣势就是,随着业务的增加,维护成本也在递增;还有一种方式叫scheme约束跳转,多用于h5,web与app之间的原生交互,当然app跳转app也可以,这个需要配置的东西更多,感兴趣的可以学习下
2:ARout-专为组建化而生,可以进行模块之间的调用,解决了传统Intent的不足
使用起来很简单:
首先在目标页面进行注解配置格式 @Route(path="/group/activity")
使用的话一句代码:
ARouter.getInstance().build(path).navigation();
这是不带参数的,
ARouter.getInstance().build(path).with(int.cahr.string.bundle).navigation();
带参数的
ARouter.getInstance().build(path).with(int.cahr.string.bundle).navigation(this,requestCode);
可以返回响应的
简单分析:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface Route {
/**
* Path of route
*/
String path();
……
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface Interceptor {
/**
* The priority of interceptor, ARouter will be excute them follow the priority.
*/
int priority();
/**
* The name of interceptor, may be used to generate javadoc.
*/
String name() default "Default";
}
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.CLASS)
public @interface Autowired {
// Mark param's name or service name.
String name() default "";
// If required, app will be crash when value is null.
// Primitive type wont be check!
boolean required() default false;
// Description of the field
String desc() default "No desc.";
}
主要有这么三种注解,有效期都是编译期间的;其实小伙伴们也可以猜出来,ARouter会在编译的时候将Router注解标注的activity扫描出来,并且按照定义好的模版生成装配关系的代码。
以map<K,V>形式存储,k就是咱们定义的path,后面的value包括与路径匹配的目标界面或者传递的参数;
整个的流程就是
小结:
ARouter可以通过注解自动注册并且在编译期间生成映射关系,在运行的时候就可以加载文件,通过path就可以顺利跳转到目标页面。