- 支持直接解析标准 URL 进行跳转,参数在目标页面通过 getIntent 获取
- 支持多模块工程使用
- 支持添加多个拦截器,自定义拦截顺序
- Activity 自动注册(使用 AutoRegister 实现自动注册)
- 自动处理
startActivityForResult
回调 - 支持获取 Fragment
- 支持启动 Fragment(需要设置
RouterClient#fragmentContainerIntent
,可参考 sample) - 支持 Kotlin
v3
- 支持 APG 8+
v2
- 优化 Api 调用
- 新增获取 Fragment
- 新增启动 Fragment
v1
- 完成路由功能
// settings file
pluginManagement {
repositories {
maven("https://jitpack.io")
}
}
dependencyResolutionManagement {
repositories {
maven("https://jitpack.io")
}
}
注意:插件仅支持 AGP 7.4+
// root build file
buildscript {
dependencies {
classpath("com.github.wangchenyan.crouter:crouter-plugin:${latestVersion}")
}
}
// app build file
plugins {
id("crouter-plugin")
}
注意:
2.4.0
版本开始使用ksp
,建议尽快迁移,如果仍想使用kapt
,请使用2.3.0
版本
// app build file
plugins {
id("com.google.devtools.ksp")
}
ksp {
// 使用默认
arg("moduleName", project.name)
// 默认 scheme
arg("defaultScheme", "(http|https|native|host)")
// 默认 host
arg("defaultHost", "(\\w+\\.)*host\\.com")
}
dependencies {
ksp("com.github.wangchenyan.crouter:crouter-processor:${latestVersion}")
implementation("com.github.wangchenyan.crouter:crouter-api:${latestVersion}")
}
建议在 Application 的 onCreate,或第一个 Activity 的 onCreate 中执行
CRouter.setRouterClient(
RouterClient.Builder()
// 设置登录拦截器,可选
.loginProvider { context, callback ->
CRouter.with(context)
.url("scheme://host/login")
.startForResult {
if (it.isSuccess()) {
callback.invoke()
}
}
}
// 启动 Fragment 时,需要设置 Fragment 容器 Activity 的 Intent
.fragmentContainerIntent(Intent(this, FragmentContainerActivity::class.java))
.build()
)
abstract class BaseActivity : AppCompatActivity() {
override fun getIntent(): Intent {
return RouterIntent(super.getIntent())
}
}
// path 使用正则匹配,注意转义
@Route("/target\\.html", needLogin = true)
class TargetActivity : BaseActivity() {
}
// 不关心结果
CRouter.with(this)
.url("scheme://host/target.html")
.start()
// 关心结果
CRouter.with(this)
.url("scheme://host/target.html")
.startForResult {
if (it.isSuccess("key")) {
val value = it.data?.getStringExtra("key")
alert("跳转取值", value)
}
}
更多用法请参考 sample 代码
在 CRouter 中使用拦截器和 OkHttp 的用法一致,这里以添加一个 H5 页面拦截器为例,当识别到网页链接,用内置浏览器打开
class H5Interceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val uri = request.uri()
val response = chain.proceed(request)
if (response.intent() == null && uri != null && (uri.scheme == "http" || uri.scheme == "https")) {
val context = request.context()
val intent = Intent(context, BrowserActivity::class.java)
intent.putExtra(Extra.URL, uri.toString())
return Response.Builder()
.context(context)
.request(request)
.intent(intent)
.build()
}
return response
}
}
在初始化时添加拦截器
CRouter.setRouterClient(
RouterClient.Builder()
.addInterceptor(H5Interceptor())
.build()
)
无
掘金:https://juejin.im/user/2313028193754168
微博:https://weibo.com/wangchenyan1993
Copyright 2019 wangchenyan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.