Android组件化路由,ARouter框架在Kotlin中的用法(跨模块跳转、登录拦截、错误重定向)

GitHub

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'
}
  • 其他用到ARouter注解的模块中,也需要加入
    //kotlin中使用kapt,ARouter的注解处理器
    kapt 'com.alibaba:arouter-compiler:1.2.2'
  • (可选)在项目的根build.gradle中,推荐导入该插件,可以在编译阶段完成初始化时的事情,可以加快app的启动
    dependencies {
     
    	...
        classpath "com.alibaba:arouter-register:1.0.2"
    }

Base模块下增加接口IIProvider

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)
}

app模块中实现IIProvider 接口


@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()
    }

}

定义几个页面

  • MainActivity
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")}")
    }
}
  • LoginActivity

@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"
  • EditActivity(低层模块,Editor模块中)
    • 传入extras = RouteFlag.FLAG_LOGIN用于登录拦截,进入该页面需要先登录
    • private val jumpProvider by lazy { ARouter.getInstance().navigation(IIProvider::class.java) }子模块下获取IIProvider的实例 jumpProvider
@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()
    }
}

生成的文件的目录

Android组件化路由,ARouter框架在Kotlin中的用法(跨模块跳转、登录拦截、错误重定向)_第1张图片

使用中的问题

  • const val PATH = “/app/main”
    const val PATH = “/editor/edit”
    不同模块下的页面要分配在不同的group中,例如app下的MainActivity定义group为app,editor模块下的EditActivity定义group为editor

参考资料

  • 谈谈App的统一跳转和ARouter

你可能感兴趣的:(kotlin)