MainActivity has a NavigationDrawer and each navigation menu brings Fragment instead of new Activity.
There is settings fragment and if I change order of the navigation menu it should be reflected immediately to NavigationDrawerFragment.
I post event in SettingsFragment, however it did not appear on NavigationDrawerFragment.
I made a AndroidBus extends Bus
public class AndroidBus extends Bus {
private final Handler mainThread = new Handler(Looper.getMainLooper());
public AndroidBus() {
super(ThreadEnforcer.ANY);
}
@Override
public void post(final Object event) {
if (BuildConfig.DEBUG) Ln.d("BUS: SYNC current thread="+Thread.currentThread().getName()+", post=" + event + " bus=" + this);
if (Looper.myLooper() == Looper.getMainLooper()) {
super.post(event);
} else {
mainThread.post(new Runnable() {
@Override
public void run() {
post(event);
}
});
}
}
@Override
public void register(Object object) {
super.register(object);
if (BuildConfig.DEBUG) Ln.d("BUS: SYNC current thread="+Thread.currentThread().getName()+", register=" + object + " bus=" + this);
}
@Override
public void unregister(Object object) {
super.unregister(object);
if (BuildConfig.DEBUG) Ln.d("BUS: SYNC current thread="+Thread.currentThread().getName()+", unregister=" + object + " bus=" + this);
}
}
and I inject bus object to each fragment by Dagger and I register fragment in onActivityCreated, and unregister it onDestroyView.
If I post event it is not delivered and I see DeadEvent log.
08-07 11:00:27.203 3519-3519/com.test.app.debug D//AndroidBus.java:40﹕ main BUS: SYNC current thread=main, register=com.test.app.ui.MainActivity@536fa3b0 bus=[Bus "default"]
08-07 11:00:27.231 3519-3519/com.test.app.debug D//AndroidBus.java:40﹕ main BUS: SYNC current thread=main, register=NavigationDrawerFragment{536b79a4 #0 id=0x7f0a0072} bus=[Bus "default"]
08-07 11:00:27.247 3519-3519/com.test.app.debug D//MainActivity.java:127﹕ main SYNC: register: bus=[Bus "default"]
08-07 11:00:27.251 3519-3519/com.test.app.debug D//AndroidBus.java:40﹕ main BUS: SYNC current thread=main, register=SettingsFragment{536b7a2c #1 id=0x7f0a0071} bus=[Bus "default"]
08-07 11:00:31.415 3519-3519/com.test.app.debug D//AndroidBus.java:24﹕ main BUS: SYNC current thread=main, post=com.test.app.events.SettingsUpdatedEvent@536d1aa4 bus=[Bus "default"]
08-07 11:00:31.415 3519-3519/com.test.app.debug D//AndroidBus.java:24﹕ main BUS: SYNC current thread=main, post=com.squareup.otto.DeadEvent@5352027c bus=[Bus "default"]
I register MainActivity also in onCreate method, if I subscribe same event in MainActivity it receives the event.
Thank you for reading this and I hope someone enlighten me about this.
I revisited this problem, and found my stupidity. The reason is that I used different @Subscribe
annotation. It could happen when you use both Otto
and Guava
libraries. Therefore, beware of this when you use both libraries in your Android app.
-import com.google.common.eventbus.Subscribe;
+import com.squareup.otto.Subscribe;
I'd also like to add to the conversation that you CANNOT declare @Subscribe
methods in an interface
or super class
, they will not be called.
From the Otto
docs:
Registering will only find methods on the immediate class type. Unlike the Guava event bus, Otto will not traverse the class hierarchy and add methods from base classes or interfaces that are annotated. This is an explicit design decision to improve performance of the library as well as keep your code simple and unambiguous.
EventBus
by greenrobot supports this feature:
https://github.com/greenrobot/EventBus
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With