Skip to content
xiaojinzi123 edited this page Jun 26, 2019 · 6 revisions

视频讲解

RouterAnno

这个注解功能是标记一个可路由的界面或者方法,不懂没关系,直接看下面的范例
首先看下注解中有什么

@RouterAnno(
        host = "component1",
        path = "test",
        // 拦截器的 class 数组
        interceptors = {Component1Interceptor1.class, Component1Interceptor2.class},
        // 拦截器的名称数组
        interceptorNames = "user.login",
        // 将来的版本中会计划用于生成文档
        desc = "业务组件1的测试界面"
)
  • host 表示模块,value表示path,
  • interceptorsinterceptorNames表示页面拦截器,详情见解释,两者的区别就是一个是填写Class属性,另一种填写的是一个字符串
  • desc 表示描述,但是目前没用

标记在 Activity

@RouterAnno(
        host = "component1",
        path = "test",
        interceptors = {Component1Interceptor1.class, Component1Interceptor2.class},
        interceptorNames = "user.login",
        desc = "业务组件1的测试界面"
)
public class Component1TestAct extends AppCompatActivity{
// ........
}

标记在静态方法上,方法允许抛出任何 Exception

RouterRequest 对象中有本次路由所有你所需要的参数值

方法必须返回 Intent

public class Component1TestAct extends AppCompatActivity{
      @RouterAnno(
            host = ModuleConfig.Module1.NAME,
            path = ModuleConfig.Module1.TEST_QUERY
      )
      public static Intent createIntent(RouterRequest request) {
            Intent intent = new Intent(request.getRawContext(), Component1TestAct.class);
            return intent;
      }
}

你甚至可以把一个系统 App 详情页面'标记'成为一个可路由的界面的代码

* 系统 App 详情
*
* @param request
* @return
*/
@RouterAnno(
      host = ModuleConfig.System.NAME, 
      path = ModuleConfig.System.SYSTEM_APP_DETAIL
)
public static Intent appDetail(@NonNull RouterRequest request) {
        Activity act = request.getActivity();
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + request.getRawContext().getPackageName()));
        return intent;
}
}

RouterAnno 总结

由于可以自定义 Intent ,那么天然就拥有如下的优势

  • 可以囊括所有的第三方界面和系统的界面到路由表中,这样子跳转到系统或者第三方的界面的就可以使用组件化带来的优势,并且可以使用各种拦截器,尤其是页面拦截器(解释)
  • 自定义 Intent 你可以拿到 Intent 做一些操作,这是一些组件化方案不能够的,
    在使用路由跳转的 intentConsumer 方法也可以拿到 Intent
Clone this wiki locally