RxRouter
一個輕量級、簡單、智能并且強大的安卓路由庫
Getting started
添加依賴
在build.gradle文件中添加以下依賴:
dependencies {
implementation 'zlc.season:rxrouter:x.y.z'
annotationProcessor 'zlc.season:rxrouter-compiler:x.y.z'
}
(替換上面的 x 、 y 和 z為最新的版本號)
如果使用 Kotlin ,用 kapt 替換 annotationProcessor
Hello World
首先在我們需要路由的Activity上添加 @Url 注解:
@Url("this is a url")
class UrlActivity : AppCompatActivity() {
...
}
然后創(chuàng)建一個被 @Router 注解的類,用來告訴RxRouter這里有一個路由器:
@Router
class MainRouter{
}
這個類不需要有任何其余的代碼,RxRouter會根據(jù)這個類的類名自動生成一個 RouterProvider ,比如這里的 MainRouter 將會生成 MainRouterProvider .
接著我們需要把這些路由器添加到 RxRouterProviders 中:
class CustomApplication : Application() {
override fun onCreate() {
super.onCreate()
RxRouterProviders.add(MainRouterProvider())
}
}
最后,就可以開始我們的表演了:
RxRouter.of(context)
.route("this is a uri")
.subscribe()
參數(shù)傳遞
攜帶參數(shù)跳轉(zhuǎn):
通過with方法,你可以給本次路由添加一系列參數(shù).
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 方法了
想要獲取跳轉(zhuǎn)返回的值?再也不用寫一大堆 onActivityResult 方法了!鏈?zhǔn)秸{(diào)用,一步到位!
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()
}
}
如果有結(jié)果返回,在subscribe中處理就行了.
Class 跳轉(zhuǎn)
不想用Url注解?沒問題,RxRouter同樣支持原始的指定類名的跳轉(zhuǎn)方式,和url跳轉(zhuǎn)的方式相同:
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 跳轉(zhuǎn)
同樣的,RxRouter也支持系統(tǒng)的Action和自定義的Action跳轉(zhuǎn).
自定義Action跳轉(zhuǎn):
<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()
})
系統(tǒng)Action跳轉(zhuǎn):
//撥打電話
RxRouter.of(this)
.addUri(Uri.parse("tel:123456"))
.routeSystemAction(Intent.ACTION_DIAL)
.subscribe()
//發(fā)送短信
val bundle = Bundle()
bundle.putString("sms_body", "這是信息內(nèi)容")
RxRouter.of(this)
.addUri(Uri.parse("smsto:10086"))
.with(bundle)
.routeSystemAction(Intent.ACTION_SENDTO)
.subscribe()
防火墻
RxRouter擁有一個小巧而強大的防火墻,能夠在路由之前根據(jù)防火墻的規(guī)則進行攔截,您可以添加一個或者多個防火墻.
//創(chuàng)建一個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.