Otto(Event bus), send event fragment to fragment but not receiving

Chk0nDanger picture Chk0nDanger · Aug 7, 2014 · Viewed 7.6k times · Source

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.

Answer

Chk0nDanger picture Chk0nDanger · Sep 1, 2014

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;