RxRouter -- 一个轻量级、简单、智能而且强大的安卓路由库

RxRouter

一个轻量级、简单、智能而且强大的安卓路由库java

Github地址android

Getting started

添加依赖

在build.gradle文件中添加如下依赖:git

dependencies {
	implementation 'zlc.season:rxrouter:x.y.z'
	annotationProcessor 'zlc.season:rxrouter-compiler:x.y.z'
}
复制代码

若是使用 Kotlin ,用 kapt 替换 annotationProcessorgithub

Hello World

首先在咱们须要路由的Activity上添加 @Url 注解:express

@Url("this is a url")
class UrlActivity : AppCompatActivity() {
    ...
}
复制代码

而后建立一个被 @Router 注解的类,用来告诉RxRouter这里有一个路由器:apache

@Router
class MainRouter{
}
复制代码

这个类不须要有任何其他的代码,RxRouter会根据这个类的类名自动生成一个 RouterProvider ,好比这里的 MainRouter 将会生成 MainRouterProvider .bash

接着咱们须要把这些路由器添加到 RxRouterProviders 中:app

class CustomApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        RxRouterProviders.add(MainRouterProvider())
    }
}
复制代码

最后,就能够开始咱们的表演了:less

RxRouter.of(context)
        .route("this is a uri")
        .subscribe()
复制代码

参数传递

携带参数跳转:ide

经过with方法,你能够给本次路由添加一系列参数.

RxRouter.of(context)
        .with(10)                         	//int value
        .with(true)							//boolean value
        .with(20.12)						//double value
        .with("this is a string value")		//string value
        .with(Bundle())						//Bundle value
        .route("this is a uri")
        .subscribe()
复制代码

再也不须要 onActivityResult 方法了

想要获取跳转返回的值?不再用写一大堆 onActivityResult 方法了!链式调用,一步到位!

RxRouter.of(context)
		.with(false)
        .with(2000)
        .with(9999999999999999)
        .route("this is a uri")
        .subscribe {
            if (it.resultCode == Activity.RESULT_OK) {
                val intent = it.data
                val stringResult = intent.getStringExtra("result")
                result_text.text = stringResult
                stringResult.toast()
            }
        }
复制代码

若是有结果返回,在subscribe中处理就好了.

Class 跳转

不想用Url注解?没问题,RxRouter一样支持原始的指定类名的跳转方式,和url跳转的方式相同:

RxRouter.of(context)
        .routeClass(ClassForResultActivity::class.java)
        .subscribe{
            if (it.resultCode == Activity.RESULT_OK) {
                val intent = it.data
                val stringResult = intent.getStringExtra("result")
                result_text.text = stringResult
                stringResult.toast()
            }
        }
复制代码

Action 跳转

一样的,RxRouter也支持系统的Action和自定义的Action跳转.

自定义Action跳转:

<activity android:name=".ActionActivity">
    <intent-filter>
        <action android:name="zlc.season.sample.action" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
复制代码
RxRouter.of(context)
        .routeAction("zlc.season.sample.action")
        .subscribe({
            "no result".toast()
        }, {
            it.message?.toast()
        })
复制代码

系统Action跳转:

//拨打电话
RxRouter.of(this)
        .addUri(Uri.parse("tel:123456"))
        .routeSystemAction(Intent.ACTION_DIAL)
        .subscribe()

//发送短信
val bundle = Bundle()
bundle.putString("sms_body", "这是信息内容")
RxRouter.of(this)
        .addUri(Uri.parse("smsto:10086"))
        .with(bundle)
        .routeSystemAction(Intent.ACTION_SENDTO)
        .subscribe()
复制代码

防火墙

RxRouter拥有一个小巧而强大的防火墙,可以在路由以前根据防火墙的规则进行拦截,您能够添加一个或者多个防火墙.

//建立一个LoginFirewall
class LoginFirewall : Firewall {
    override fun allow(datagram: Datagram): Boolean {
        if (notLogin) {
            "您尚未登陆,请先登陆".toast()
            return false
        }
        return true
    }
}

//将Firewall添加到路由中
RxRouter.of(this)
        .addFirewall(LoginFirewall())
        .route("this is a url")
        .subscribe()
复制代码

License

Copyright 2018 Season.Zlc

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.
复制代码
相关文章
相关标签/搜索