I have two Activities, I am trying to send event from activity A to activity B without entering to activity B.
I am trying to do it with sticky eventbus, the problem is the event does not get trigger without me entering to the activity B. I don't know that the problem is, here is my code in activity B
override fun onStart() {
super.onStart()
registerEventBus()
}
override fun onDestroy() {
super.onDestroy()
unregisterEventBus()
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
fun onStickyEventBus(case: EventBusType?) {
when (case) {
EventBusType.ONE_FOR_PAYMENT_COMPLETED ->{
// do something
removeEvent(case)
}
else -> {}
}
}
And the code on my activity A:
...
postEvent(EventBusType.ONE_FOR_PAYMENT_COMPLETED,sticky = true)
the extensions:
fun postEvent(event: Any?, sticky: Boolean = false) {
if (sticky) EventBus.getDefault().postSticky(event)
else EventBus.getDefault().post(event)
}
fun removeEvent(event: Any?) {
EventBus.getDefault().removeStickyEvent(event)
}
fun Activity.registerEventBus() {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this)
}
}
fun Activity.unregisterEventBus() {
if (EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this)
}
}
I have two Activities, I am trying to send event from activity A to activity B without entering to activity B.
I am trying to do it with sticky eventbus, the problem is the event does not get trigger without me entering to the activity B. I don't know that the problem is, here is my code in activity B
override fun onStart() {
super.onStart()
registerEventBus()
}
override fun onDestroy() {
super.onDestroy()
unregisterEventBus()
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
fun onStickyEventBus(case: EventBusType?) {
when (case) {
EventBusType.ONE_FOR_PAYMENT_COMPLETED ->{
// do something
removeEvent(case)
}
else -> {}
}
}
And the code on my activity A:
...
postEvent(EventBusType.ONE_FOR_PAYMENT_COMPLETED,sticky = true)
the extensions:
fun postEvent(event: Any?, sticky: Boolean = false) {
if (sticky) EventBus.getDefault().postSticky(event)
else EventBus.getDefault().post(event)
}
fun removeEvent(event: Any?) {
EventBus.getDefault().removeStickyEvent(event)
}
fun Activity.registerEventBus() {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this)
}
}
fun Activity.unregisterEventBus() {
if (EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this)
}
}
Share
Improve this question
asked Nov 21, 2024 at 7:49
Flutra DemajFlutra Demaj
11 bronze badge
1 Answer
Reset to default 0Most likely you are already registered to the EventBus because you unregister in OnDestroy and that will be called only when the Activity ends. It's a good habit to register/unregister in mirroring lifecycle events (e.g. OnStart/OnPause). Moving the unregisterEventBus() to OnPause() should fix it.
override fun onPause() {
super.onPause()
unregisterEventBus()
}
Another solution would be to pass data over using Intents https://www.geeksfeeks./how-to-send-data-from-one-activity-to-second-activity-in-android/