## 功能说明 # 路由跳转 ## path ```kotlin //RouterUri注解,拦截器按定义顺序调用 @RouterUri(path = ["/test"], interceptors = [TestUriInterceptor1::class, TestUriInterceptor2::class]) class TestRouterBindActivity : AppCompatActivity(R.layout.activity_router_test) { //BindExtra注解字段必须为public @BindExtra(name = "extra_test") var extraTest: String? = null private val binding by viewBinding(ActivityRouterTestBinding::inflate) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Router.bind(this) //Activity必须手动调用才会自动解析BindExtra } } //Router跳转 Router.build(context, "/test") .putExtra("extra_test", "测试extra") .start() ``` ## scheme + host ```kotlin //scheme和host必须同时设置 @RouterUri( scheme = "weparty", host = "test", interceptors = [TestUriInterceptor1::class, TestUriInterceptor2::class] ) class TestUriHandler : UriHandler() { //BindExtra注解字段必须为public,不需要调用bind会自动绑定 @BindExtra(name = "extra_test") var extraTest: String? = null override fun canHandle(request: UriRequest): Boolean { return true } override fun handle(request: UriRequest) { } } //Router跳转 Router.build(context, "weparty://test") .putExtra("extra_test", "测试extra") .start() ``` ## scheme + host + path ```kotlin //scheme和host必须同时设置 @RouterUri( scheme = "weparty", host = "test", path = ["/path1", "/path2"], interceptors = [TestUriInterceptor1::class, TestUriInterceptor2::class] ) class TestUriHandler : UriHandler() { //BindExtra注解字段必须为public,不需要调用bind会自动绑定 @BindExtra(name = "extra_test") var extraTest: String? = null override fun canHandle(request: UriRequest): Boolean { return true } override fun handle(request: UriRequest) { } } //Router跳转 Router.build(context, "weparty://test/path1") .putExtra("extra_test", "测试extra") .start() Router.build(context, "weparty://test/path2") .putExtra("extra_test", "测试extra") .start() ``` ## regex regex优先级低于sheme+host+path,即优先匹配scheme+host+path ```kotlin //regex定义正则表达式,priority定义优先级,数字越大优先级越高 @RouterUri( regex = "http(s)?://(.*\\.)?(weparty)\\.(com|info|cn).*", priority = 2 ) class WebViewActivity : AppCompatActivity() //Router跳转 Router.build(context, "https://weparty.com/test") .putExtra("extra_test", "测试extra") .start() ``` ## 拦截器 ```kotlin //拦截器在触发handler之前执行 class TestUriInterceptor1 : UriInterceptor { override fun intercept(chain: UriInterceptor.Chain) { val request = chain.request() chain.proceed(request) //继续请求 chain.abort() //终止请求 } } ``` # DeepLink支持 ```kotlin //注册RouterDeepLinkActivity //触发deeplink,会执行TestUriHandler adb shell am start -W -a android.intent.action.VIEW -d "weparty://test" com.adealink.weparty ``` # 获取实例 ```kotlin @RouterUri(path = ["/bind_fragment"], desc = "测试绑定Fragment") class BindFragment : Fragment() { @BindExtra(name = "extra_test") var extraTest: String? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Router.bind(this) } } //获取实例 val fragment = Router.getRouterInstance("/bind_fragment") ```