Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since they are two activities, <code>ActivitySubscriber</code> posts the event while <code>ActivityReceiver</code> is still not created, or is in stall mode (<code>onStop()</code>). You need to use sticky events, i.e. </p> <ul> <li><code>ActivitySubscriber.postSticky(...)</code></li> </ul> <p>And for ActivityReceiver you have two options:</p> <ul> <li><code>EventBus.getDefault().register(this)</code> and somewhere after that <code>EventBus.getDefault().getStickyEvent()</code></li> <li><code>EventBus.getDefault().registerSticky()</code> and then using regular <code>EventBus.getDefault().onEvent(...)</code></li> </ul> <p><strong>Updated:</strong> EventBus 3.0 changes the way to subscribe.</p> <p>There is no need of method names which end up with specific suffixes but rather annotations.</p> <p>How to use version 3:</p> <pre><code>//// 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, i.e. 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); } } </code></pre> <p>For more details and comparison of version 3:</p> <ul> <li><a href="http://androiddevblog.com/eventbus-3-droidcon/" rel="noreferrer">http://androiddevblog.com/eventbus-3-droidcon/</a></li> <li><a href="http://androiddevblog.com/wordpress/wp-content/uploads/EventBus3.pdf" rel="noreferrer">http://androiddevblog.com/wordpress/wp-content/uploads/EventBus3.pdf</a></li> </ul> <p>Some details extracted from the sources:</p> <ul> <li><code>ThreadMode.PostThread</code> <blockquote> <p>Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.</p> </blockquote></li> <li><code>ThreadMode.MainThread</code> <blockquote> <p>Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is the main thread, event handler methods will be called directly. Event handlers using this mode must return quickly to avoid blocking the main thread.</p> </blockquote></li> <li><code>ThreadMode.BackgroundThread</code> <blockquote> <p>Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single background thread, that will deliver all its events sequentially. Event handlers using this mode should try to return quickly to avoid blocking the background thread.</p> </blockquote></li> <li><code>ThreadMode.Async</code> <blockquote> <p>Event handler methods are called in a separate thread. This is always independent from the posting thread and the main thread. Posting events never wait for event handler methods using this mode. Event handler methods should use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.</p> </blockquote></li> <li><strong>default</strong> values for <code>@Subscribe</code> <ul> <li><code>threadMode = ThreadMode.PostThread</code></li> <li><code>sticky = false</code> - If true, delivers the most recent sticky event (posted with <code>de.greenrobot.event.EventBus.postSticky(Object)</code> to this subscriber (if event available)</li> <li><code>priority = 0</code> - Subscriber priority to influence the order of event delivery. Within the same delivery thread, higher priority subscribers will receive events before others with a lower priority. The default priority is 0. Note: the priority does <em>NOT</em> affect the order of delivery among subscribers with different thread modes.</li> </ul></li> </ul> <h1>Edit 2</h1> <p>There is a dedicated site now for any Greenrobot EventBus questions from the creator of the lib:</p> <p><a href="http://greenrobot.org/eventbus/" rel="noreferrer">http://greenrobot.org/eventbus/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload