maven库
implementation 'com.hwangjr.rxbus:rxbus:2.0.0'
场景:A界面跳转到B界面,B界面发送事件到A界面,个人感觉和EventBus一模一样
界面A代码
class AActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_a)
initView()
}
private fun initView() {
RxBus.get().register(this)
btnA.setOnClickListener {
startActivity(Intent(this, BActivity::class.java))
}
}
@Subscribe(thread = EventThread.MAIN_THREAD)
fun onMsgA(msgA: MsgA) {
Log.e("AActivity", msgA.getValue())
}
@Subscribe(
thread = EventThread.MAIN_THREAD,
tags = [Tag("tag")]
)
fun onTagValue(value: String) {
Log.e("AActivity", value)
}
override fun onDestroy() {
super.onDestroy()
RxBus.get().unregister(this)
}
}
界面B代码
class BActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_b)
initView()
}
private fun initView() {
btnB.setOnClickListener {
RxBus.get().post(MsgA("我是B界面传过来的实体数据"))
}
btnB2.setOnClickListener {
RxBus.get().post("tag","我是B界面传过来的实体定向数据")
}
}
}
MsgA.kt代码
class MsgA(value:String) {
var msg = value
fun getValue():String{
return this.msg
}
fun setValue(v: String){
this.msg = v
}
}