What is the best place to register and unregister to an event bus (like otto, EventBus, or tinybus) in an Activity and why?
Otto's example uses onResume()-onPause(), EventBus's mentions onStart()-onStop(), and we needed to use onCreate()-onDestroy() in our app to update the activity's UI even when it was in the background. So I guess it can be any of the three depending on the nature of the events and their handling, but I was wondering if there is anything more to it that should be considered.
An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .
onStop() When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen.
There are two lifecycle methods that handle when the activity is paused and when it becomes active again: onPause() and onResume() . onPause() gets called when your activity is visible but another activity has the focus. onResume() is called immediately before your activity is about to start interacting with the user.
First of all, its not a objective question rather its a subjective one and will draw lots of arguments based on arguments.
From my experience, We used Otto in one of our project. We followed onResume()-onPause() which served us very good. It also makes sense cause we should register as late as possible & deregister as fast as possible while using an event bus.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With