What is the best place to register and unregister to an event bus (like otto, EventBus, or tinybus) in an Activity and why?
Form EventBus Documentation that I found and it's works fine for me:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
And if you need to send the EventBus reference to the child then:
private EventBus eventBus = EventBus.getDefault();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.......
}
@Override
public void onStart() {
super.onStart();
if(!eventBus.isRegistered(this)){
eventBus.register(this);
}else{
Log.e(TAG, "EventBus is registered");
}
}
@Override
public void onStop() {
super.onStop();
if(eventBus.isRegistered(this)){
eventBus.unregister(this);
}else{
Log.e(TAG, "EventBus is not registered");
}
}