Reputation: 4617
I am trying to add event bus into my project, because this is really great pattern to follow in order to make code more readable and application robust.
I need to listen to the Activity lifecycle methods.
Let me explain why do I need this.
I have activity, inside this activity there is running thread (or threads) that is tied to this activity exactly to the main UI thread posting some UI changes to the main queue.
When activity is going to be destroyed or paused I need to pause thread and resume it when activity is on screen again.
Of course I can pause/resume thread directly in onDestroy and onStart methods, but there are some another components that also need to listen to activity lifecycle methods.
There are my questions :
Is it correct use EventBus in this case, or it is better just to do everything in activity lifecycle methods directly without using event bus. And using event bus in activity lifecycle is ambiguous and incorrect.
public class ExampleActivity extends Activity {
private EventBus bus = EventBus.getDefault();
private TextView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
bus.register(this);
bus.post(new OnCreateEvent("Activity has been born"));
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
// Unregister
bus.post(new OnDestroyEvent("Activity is going to die"));
bus.unregister(this);
super.onDestroy();
}
} The one advantage I see here is that you haven't to add some logic into activity lifecycle methods each time your part of activity needs this. Activity content can be quite complex,also fragments can be used, of course you can use fragment lifecycle methods also. But I wonder if fragment will not be destroyed to the time when activity lifecycle methods will be called, I haven't tested this yet. So the main question here what is better to use native hook methods mechanism to do required stuff here or to use event bus.
Next questions are not directly related to main question. Question are about the way in that EventBus
(EventBus Link) library for android is implemented.
Why do not make listeners (bus.register(this);
) to implement interface for instance Subscribe with one method onEvent(BusEvent event);
. And why not to make this method accept some basic class BusEvent
. In library implementation reflection is being used each time when event occurs and method onEvent
is called. I don't see any advantages in this approach and reflection is quite heavy thing in Java.
I would be very grateful for any answers or advice.
Upvotes: 0
Views: 1101
Reputation: 4651
You can do this and it will be a nice solution if u have multiple threads listening to multiple activities
, but u need to pass the activity
or some id
to the event so the thread will know which activity stopped/started. If you use only one activity and have threads that handle it then I wouldn't use eventbus
. I use eventbus
only when I have multiple subscribers and from different places in the code and not this case where its one activity
and a few threads.
Actually after going trough the source code, I think that u are right. But I read somewhere that very soon they will publish EventBus
version 3.0 soon. Can't find right now so I hope I didn't imagine it :).
It will be faster and better, maybe even like you proposed.
Upvotes: 1