2

I have started to use the event bus library Otto for updating the interface (activities) from different components in my Android application. For instance, when a change to a Model class is made, I post to the event bus or if an AsyncTask has finished, I post an event to the event bus in the onPostExecute method.

So far I am using one event bus for user interface updates. I noticed that even paused activities receive these events yet. In the documentation it states

The paused activity does not receive user input and cannot execute any code.

Which I find controversial, I can only explain this, that this code is of course executed on a different thread, but still in the activity.

My question is, does this usage result in any disadvantages? Having multiple activies being paused, executing events, updating elements of (paused) activities or ignoring them. Does this result in a noticeable overhead or can I ignore it?

Is there a different approach when one wants to use event bus for updating the interface?

Mahoni
  • 7,088
  • 17
  • 58
  • 115
  • Are you unregistering your activities from the Bus in `onPause`? It doesn't sound like you are if you have noticed paused activities getting updated. One disadvantage might be that paused activities may be killed, so if you are relying on receiving events while paused for UI/data consistency, you may run into issues. – swanson Jun 24 '14 at 19:58
  • @swanson Exactly, not yet, but with this in mind the whole thing seems to be a lot more reasonable. – Mahoni Jun 27 '14 at 09:58

1 Answers1

4

Does this result in a noticeable overhead or can I ignore it?

That is impossible to say in the abstract. It will depend on what work you are doing and how often you are doing it.

Is there a different approach when one wants to use event bus for updating the interface?

There is no requirement that your activities and fragments be listening for events while they are in the background. For example, you can register for events in onResume() and unregister in onPause(). You can then use other techniques (e.g., Otto's @Producer pattern) to update your activity's/fragment's UI en masse when it comes back to the foreground in onResume().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    @beerBear: Similar alternative to what? If you mean the `@Producer` pattern, you can try sticky events in greenrobot's EventBus. – CommonsWare Feb 01 '15 at 14:08