Since they are two actions, the ActivitySubscriber publishes the event, and the ActivityReceiver is still not created or is in standby mode ( onStop() ). You need to use sticky events i.e.
ActivitySubscriber.postSticky(...)
And for ActivityReceiver you have two options:
EventBus.getDefault().register(this) and somewhere after that EventBus.getDefault().getStickyEvent()EventBus.getDefault().registerSticky() and then using regular EventBus.getDefault().onEvent(...)
Updated: EventBus 3.0 changes the way you subscribe.
There is no need for method names that end with specific suffixes, but rather annotations.
How to use version 3:
//// in your build.gradle compile 'de.greenrobot:eventbus:3.0.0-beta1' // alternatively you can target latest whatever currently // compile 'de.greenrobot:eventbus:+' //// from a class which needs to dispatch an event // posting an event is as before, no changes // here we dispatch a sticky event EventBus.getDefault().postSticky(myStickyEvent); //// from your class which needs to listen // method name can be any name // any of subscribe params is optional, ie can use just @Subscribe @Subscribe(threadMode = ThreadMode.MainThread, sticky = true, priority = 1) public void onEventBusEvent(@Nullable final StickyEvent stickyEvent) { if (stickyEvent != null) { ... // optionally you can clean your sticky event in different ways EventBus.getDefault().removeAllStickyEvents(); // EventBus.getDefault().removeStickyEvent(stickyEvent); // EventBus.getDefault().removeStickyEvent(StickyEvent.class); } }
More details and comparison of version 3:
Some details are extracted from sources:
ThreadMode.PostThreadThe subscriber will be called in the same thread that sends the event. This is the default value. Event delivery is the least expensive since it completely eliminates thread switching. Thus, this is the recommended mode for simple tasks that are known to be completed, very short, without requiring a main thread. Event handlers using this mode should return quickly so as not to block the wiring flow, which may be the main thread.
ThreadMode.MainThreadThe caller will be called in the main Android theme (sometimes called the user interface thread). If the posting thread is the main thread, the event handler methods will be called directly. Event handlers using this mode should return quickly to avoid blocking the main thread.
ThreadMode.BackgroundThreadThe subscriber will be called in the background thread. If the publication stream is not the main stream, the event handler methods will be called directly in the posting stream. If the posting thread is the main thread, EventBus uses a single background thread that will transmit all its events sequentially. Event handlers using this mode should try to quickly return to avoid blocking the background thread.
ThreadMode.AsyncEvent handler methods are called in a separate thread. It always depends on the wiring flow and main flow. Posting events never wait for event handler methods using this mode. Event handler methods should use this mode if they can take some time to execute, for example. to access the network. Avoid running a large number of long asynchronous handler methods at the same time to limit the number of simultaneous threads. EventBus uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
- default values for
@SubscribethreadMode = ThreadMode.PostThreadsticky = false - If true, delivers the most recent sticky event (dispatched using de.greenrobot.event.EventBus.postSticky(Object) this subscriber (if an event is available)priority = 0 - subscriber priority affects the order in which events are delivered. Within the same delivery line, subscribers with a higher priority will receive events before others with a lower priority. The default priority is 0. Note: priority does NOT affect the delivery order among subscribers with different flow modes.
Edit 2
Now you have a dedicated site for any GreenBot EventBus questions from the creator of lib:
http://greenrobot.org/eventbus/
source share