https://github.com/alibaba/ARouter
id 'kotlin-kapt'
android {
defaultConfig {
...
kapt {
arguments {
//根据模块名来命名路由根节点
arg("AROUTER_MODULE_NAME", project.getName())
//生成Json文件
arg("AROUTER_GENERATE_DOC", "enable")
}
}
}
}
dependencies {
...
//ARouter的api
implementation 'com.alibaba:arouter-api:1.5.1'
//kotlin中使用kapt,ARouter的注解处理器
kapt 'com.alibaba:arouter-compiler:1.2.2'
}
//kotlin中使用kapt,ARouter的注解处理器
kapt 'com.alibaba:arouter-compiler:1.2.2'
dependencies {
...
classpath "com.alibaba:arouter-register:1.0.2"
}
package com.zhangyu.router
import com.alibaba.android.arouter.facade.template.IProvider
interface IIProvider : IProvider {
fun startLoginActivity(from: String)
fun startMainActivity(from: String)
fun startSearchActivity(from: String)
fun startEditorActivity(from: String)
}
object RouteFlag {
//为什么可以使用route注解的extra参数为目标页指定属性
//因为Int数值在内存中占4个字节,每个字节占8位,所以利用extras字段我们可以为目标页指定32个开关
const val FLAG_LOGIN: Int = 0x01
const val FLAG_AUTHORITY = FLAG_LOGIN.shl(1)//等同于java中的FLAG_LOGIN<<1
const val FLAG_VIP = FLAG_AUTHORITY.shl(1)
}
@Route(path = JumpProvider.PATH)
class JumpProvider : IIProvider {
companion object {
const val PATH = "/service/provider"
}
override fun init(context: Context?) {
}
override fun startLoginActivity(from: String) {
ARouter.getInstance().build(LoginActivity.PATH)
.navigation()
}
override fun startMainActivity(from: String) {
ARouter.getInstance().build(MainActivity.PATH)
.navigation()
}
override fun startSearchActivity(from: String) {
ARouter.getInstance().build(SearchActivity.PATH)
.navigation()
}
override fun startEditorActivity(from: String) {
ARouter.getInstance().build(EditActivity.PATH)
.navigation()
}
}
private const val TAG = "MainActivity"
@Route(path = MainActivity.PATH)
class MainActivity : AppCompatActivity() {
companion object {
const val PATH = "/app/main"
}
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.btGotoLogin.setOnClickListener {
JumpProvider().startLoginActivity("MainActivity")
}
binding.btGotoEditor.setOnClickListener {
JumpProvider().startEditorActivity("MainActivity")
}
binding.btGotoSearch.setOnClickListener {
JumpProvider().startSearchActivity("MainActivity")
}
binding.btGotoError.setOnClickListener {
//传入一个错误的路径
ARouter.getInstance()
.build("/path/unknown")
.navigation()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.d(TAG, "onActivityResult: ${data?.getStringExtra("key")}")
}
}
@Route(path = LoginActivity.PATH)
class LoginActivity : AppCompatActivity() {
companion object {
const val PATH = "/app/login"
}
@JvmField
@Autowired
var from = ""
lateinit var binding: ActivityLoginBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
binding.lifecycleOwner = this
//设置参数的自动注入
ARouter.getInstance().inject(this)
//打印
Log.d(TAG, "onCreate: from=$from")
initView()
}
private fun initView() {
binding.btLogin.setOnClickListener {
LoginRepositoryProvider.getInstance().login()
finish()
}
binding.btLogout.setOnClickListener {
LoginRepositoryProvider.getInstance().logout()
finish()
}
}
}
private const val TAG = "LoginActivity"
@Route(path = EditActivity.PATH, extras = RouteFlag.FLAG_LOGIN)
class EditActivity : AppCompatActivity() {
companion object {
const val PATH = "/editor/edit"
}
private lateinit var binding: ActivityEditorBinding
private val jumpProvider by lazy {
ARouter.getInstance().navigation(IIProvider::class.java) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_editor)
binding.btGotoSearch.setOnClickListener {
jumpProvider.startSearchActivity("EditorActivity")
}
}
}
//定义一个拦截器,需要name随便写一个即可
@Interceptor(name = "/service/interceptor", priority = 9)
class GlobalInterceptor : IInterceptor {
private var context: Context? = null
override fun init(context: Context?) {
this.context = context
}
override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
val flag = postcard?.extra
if (flag?.and(RouteFlag.FLAG_LOGIN) != 0) {
//判断是否已经登录
val isLogin = LoginRepositoryProvider.getInstance().isLogin()
if (!isLogin) {
ARouter.getInstance().build(LoginActivity.PATH)
.navigation()
showToast("需要登录")
} else {
//已经登录不需要拦截
callback?.onContinue(postcard)
}
} else {
//不需要拦截
callback?.onContinue(postcard)
}
}
private fun showToast(msg: String) {
//拦截器在子线程中,需要切换到主线程更新UI
Handler(Looper.getMainLooper()).post {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
}
}
}
//全局降级服务,当路由的时候,目标不存在,重定向到统一错误页面
//定义一个任意的path注解即可生效
@Route(path = "/service/degrade")
class DegradeServiceImp : DegradeService {
override fun init(context: Context?) {
}
//页面没有找到时触发
override fun onLost(context: Context?, postcard: Postcard?) {
ARouter.getInstance().build(ErrorActivity.PATH)
.greenChannel()//greenChannel不会被拦截器拦截
.navigation()
}
}