I have got an activity with the view of an object received from an async HTTP request. I am using EventBus to post an event with the object and in the activity the following method:
public void onEventMainThread(MyObjectEvent event) {
EventBus.getDefault().unregister(this);
showMyObject(event.getMyObject());
}
Previously, I have registered the activity's instance in its onCreate() method:
EventBus.getDefault().register(this);
I am aware of the Activity lifecycle in Android and I think that my way to use EventBus is not right because I am registering an instance which could be destroyed by Android. I am not keen on registering an instance with such behaviour.
I could register an Application instance or an object with every onEvent() method but I would be coding a God object.
Would be a solution use sticky events by verifying getStickyEvent(MyObjectEvent) is null and then register the activity? In case of getting not null I would register the activity.
I would like to know what is the best approach to use EventBus with views in Android. Thanks in advance ;)
If you register in onCreate, please think about unregistering in onDestroy.
Also, please have a look at registerSticky, which immediately delivers an event if available.