square / otto

An enhanced Guava-based event bus with emphasis on Android support.
http://square.github.io/otto/
Apache License 2.0
5.17k stars 848 forks source link

Event not received in the Subscribe method #180

Closed felipecaldas closed 8 years ago

felipecaldas commented 8 years ago

Hi all,

First time using Otto and having some trouble getting the event to arrive at the method subscribed to it.

In Activity1, I have the line:

BusProvider.getInstance().post(mGameState);

At Activit2, I have the line:

@Subscribe public void do(MultiplayerGameState mTurnData) { }

Obviously I am sending the same object type in the post... nothing arrived at the do() method.

Is there anything else in terms of configuration I have to do? Something in XMLs?

I have also registered the Bus in the onStart method. Have also tried in the onResume method.

Nothing worked.

luaz commented 8 years ago
JakeWharton commented 8 years ago

Is the second activity registered before the first one sends the event?

felipecaldas commented 8 years ago

Thanks Jake and Luaz. I can confirm that the interface used to subscribe is com.squareup.otto.Subscribe

I confirm that register() is called in onStart() in both Activities and unregister it onStop()

The Activity2, the one that posts() is registered after Activity1. Basically, Activity1 calls Activity2 and then Activity2 sends a message to Activity1.

That's a correct pattern, right?

felipecaldas commented 8 years ago

ok, I just tried removing the unregister() call from both Activities and now I got the event... is it maybe because when Activity1 calls Activity2 via Intent, the onStop() method is called and therefore unregistering the bus?

JakeWharton commented 8 years ago

Probably, yes. Otto is not designed for communicating between activities. Since only one is displayed on the screen at a time, it's unsurprising that events get lost when each is registering and unregistering as their visibility status changes. You're much better off using the Intent system and activity result system for passing data as part of navigation flow. An event bus is for distribution of information and it should be assumed that events could always be missed.

felipecaldas commented 8 years ago

Dang, I completely thought that Otto was meant for communication between activities! That would've been really nice :) Thanks for the info Jake.

I suppose EventBus from GreenRobot also does not do this sort of job?

felipecaldas commented 8 years ago

Just tried EventBus with RegisterSticky. It is pretty much working the same way as Otto. I cannot unregister Activity1, it needs to always listen

Now the problem is that I have a GoogleApiClient class and the method that gets called from Activity2 needs it to be connected. Since that event is by-passing the onStart() of Activity1 (where mGoogleApiClient.connect() ) is, the object is not connect. And then I fail. Probably a second later, the onStart() is called and it gets connected.

I will have to think of a different architecture.