L
L
Lolzman2014-12-19 17:26:10
Android
Lolzman, 2014-12-19 17:26:10

How to handle events sent by EventBus when Android UI component is paused?

Hello,
I have been chasing the perfect architecture for an android application for several weeks in a row.
I really liked the following articles:
www.mdswanson.com/blog/2014/04/07/durable-android-...
www.birbit.com/a-recipe-for-writing-responsive-res...
As you can see, in In both posts, interaction and communication between android components is done using the Event Bus. (in one case EventBus from greendao, in the other Otto from square) . The only drawback of both posts, I think that nowhere is a word about how to handle events that were sent to an Activity or Fragment when the latter were in a paused state.
Because event bus needs to be unbind from ui components in the onPause() method, it turns out that we lose the event that was sent to them.
Do not tell me what would be the most straightforward and beautiful solution to this problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nikolai Pavlov, 2014-12-19
@gurinderu

Try to accumulate events in some kind of collection, for example linledlist or better queue (of course concurent), when events come in the background and run them in order during onResume. However, if the events are interconnected, there will be Achtung. In general, this is not such an easy task. You can try to poll the status of the service on onResume and do without events at all.

A
Alexander, 2014-12-22
@DrZ0idberg

Sticky events should help. The EventBus stores the last sticky event instance sent. Accordingly, at the start of an activity / fragment, this event can be requested and processed.
But since only the last event is stored, it will not be possible to get a whole sequence of missed events (unless they are of different types and were not all sent as sticky). In general, if such a need arises, it is probably worth revising the architecture of the application. EventBus is not a panacea - it may well be poorly suited for some tasks ...
PS: I use Greenrobot's EventBus in my projects, so all of the above applies to this implementation.

S
StanKo, 2014-12-23
@StanKo

And if you don't unbind the EventBus in onPause(), but only in onDestroy()? Yes, the activity will be paused and will not display reactions in the interface, but it will still be possible to catch and process it, and at least throw it into the collection of events.
PS Google itself states that you should not use their implementation of EventBus (which is in Guava), so it's better to use greenrobot's.

D
Dmtm, 2017-02-01
@Dmtm

you need a data layer that is independent of the UI, the
UI takes data only in the data layer
EventBus sends only notifications about changes in the data, but not the data itself
. if the activity is alive, then it takes the values ​​​​in the data layer by default, if a notification arrives during life, then it is updated from the data layer,
if the activity is restarted, then the notification is of course gone, but the data is still taken from the data layer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question