0
点赞
收藏
分享

微信扫一扫

【Android,Arouter,Kotlin】一个小例子教你使用阿里路由框架Arouter


一个小例子教你使用阿里路由框架Arouter

效果

【Android,Arouter,Kotlin】一个小例子教你使用阿里路由框架Arouter_ide
【Android,Arouter,Kotlin】一个小例子教你使用阿里路由框架Arouter_android_02
【Android,Arouter,Kotlin】一个小例子教你使用阿里路由框架Arouter_android_03

配置

​project gradle​

buildscript {
...
dependencies {
...
//配置初始化预编译插件-->
classpath 'com.alibaba:arouter-register:1.0.2'
}
}

​module gradle​

plugins {
...
//配置插件-->
id 'kotlin-kapt'
id 'com.alibaba.arouter'
}

android {
...

defaultConfig {
...
//配置选项-->
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
...
}

dependencies {
...
//配置依赖-->
implementation 'com.alibaba:arouter-api:1.5.0'
kapt 'com.alibaba:arouter-compiler:1.2.2'
}

Crazy Codeing

​Routes​

object Routes {
const val PATH_MAIN = "/main/main"
const val PATH_LOGIN = "/main/login"
const val PATH_AUTHENTICATION = "/main/authentication"
const val PATH_VIP = "/main/vip"
const val PATH_DEGRADE = "/main/degra"

const val DEGRADE_SERVICE = "/degrade/service"
const val INTERCEPTOR = "interceptor"

const val EXTRA_LOGIN = 0x01
const val EXTRA_AUTHENTICATION = EXTRA_LOGIN shl 1
const val EXTRA_VIP = EXTRA_AUTHENTICATION shl 1
}

​RouteHelper​

fun String.navigate() {
ARouter.getInstance().build(this).navigation()
}

​MainApplication​

class MainApplication : Application() {
override fun onCreate() {
super.onCreate()

if (BuildConfig.DEBUG) {
ARouter.openLog()
ARouter.openDebug()
}

ARouter.init(this)
}
}

​MainActivity​

@Route(path = Routes.PATH_MAIN)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

findViewById<Button>(R.id.main_center).setOnClickListener {
Routes.PATH_LOGIN.navigate()
}

findViewById<Button>(R.id.main_vip).setOnClickListener {
Routes.PATH_VIP.navigate()
}

findViewById<Button>(R.id.main_account).setOnClickListener {
Routes.PATH_AUTHENTICATION.navigate()
}

findViewById<Button>(R.id.main_degrade).setOnClickListener {
"/main/unknow".navigate()
}
}
}

自定义降级策略
​​​CustomDegradeService​

@Route(path = Routes.DEGRADE_SERVICE)
class CustomDegradeService : DegradeService {
override fun init(context: Context?) {
ARouter.getInstance().build(Routes.PATH_DEGRADE).greenChannel().navigation()
}

override fun onLost(context: Context?, postcard: Postcard?) {
}
}

自定义拦截器
​​​CustomInterceptor​

@Interceptor(priority = 9, name = Routes.INTERCEPTOR)
class CustomInterceptor : IInterceptor {
private var mContext: Context? = null

override fun init(context: Context?) {
this.mContext = context
}

override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
val flag = postcard!!.extra

when {
(flag and Routes.EXTRA_LOGIN) != 0 -> {
//login
callback!!.onInterrupt(RuntimeException("need login"))
showToast("请先登录")
}
(flag and Routes.EXTRA_AUTHENTICATION) != 0 -> {
//authentication
callback!!.onInterrupt(RuntimeException("need authentication"))
showToast("请先实名认证")
}
(flag and Routes.EXTRA_VIP) != 0 -> {
//vip
callback!!.onInterrupt(RuntimeException("need become vip"))
showToast("请先加入会员")
}
else -> {
callback!!.onContinue(postcard)
}
}
}

private fun showToast(content: String) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(mContext!!, content, Toast.LENGTH_SHORT).show()
}
}
}

​AuthenticationActivity​

@Route(path = Routes.PATH_AUTHENTICATION, extras = Routes.EXTRA_AUTHENTICATION)
class AuthenticationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_authentication)

findViewById<Button>(R.id.authentication_return).setOnClickListener {
Routes.PATH_MAIN.navigate()
finish()
}
}
}

​DegradeActivity​

@Route(path = Routes.PATH_DEGRADE)
class DegradeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_degrade)

findViewById<ImageView>(R.id.degrade_return).setOnClickListener {
Routes.PATH_MAIN.navigate()
finish()
}
}
}

​LoginActivity​

@Route(path = Routes.PATH_LOGIN,extras = Routes.EXTRA_LOGIN)
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)

findViewById<Button>(R.id.login_return).setOnClickListener {
Routes.PATH_MAIN.navigate()
finish()
}
}
}

​VipActivity​

@Route(path = Routes.PATH_VIP,extras = Routes.EXTRA_VIP)
class VipActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_vip)

findViewById<Button>(R.id.vip_return).setOnClickListener {
Routes.PATH_MAIN.navigate()
finish()
}
}
}

还有一些布局文件及简单的按钮控件


举报

相关推荐

0 条评论