最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Sticky eventbus only gets called when I enter to activity - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

Most 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/

发布评论

评论列表(0)

  1. 暂无评论