本文主要是介绍【Android,Arouter,Kotlin】一个小例子教你使用阿里路由框架Arouter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一个小例子教你使用阿里路由框架Arouter
效果
配置
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 = 0x01const val EXTRA_AUTHENTICATION = EXTRA_LOGIN shl 1const 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? = nulloverride fun init(context: Context?) {this.mContext = context}override fun process(postcard: Postcard?, callback: InterceptorCallback?) {val flag = postcard!!.extrawhen {(flag and Routes.EXTRA_LOGIN) != 0 -> {//logincallback!!.onInterrupt(RuntimeException("need login"))showToast("请先登录")}(flag and Routes.EXTRA_AUTHENTICATION) != 0 -> {//authenticationcallback!!.onInterrupt(RuntimeException("need authentication"))showToast("请先实名认证")}(flag and Routes.EXTRA_VIP) != 0 -> {//vipcallback!!.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()}}
}
还有一些布局文件及简单的按钮控件
这篇关于【Android,Arouter,Kotlin】一个小例子教你使用阿里路由框架Arouter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!