Which Activity lifecycle methods are best to register/unregister to event bus?

levavare picture levavare · Mar 24, 2015 · Viewed 11.1k times · Source

What is the best place to register and unregister to an event bus (like otto, EventBus, or tinybus) in an Activity and why?

  1. onCreate()-onDestroy()
  2. onStart()-onStop()
  3. onResume()-onPause()

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.

Answer

Amit K. Saha picture Amit K. Saha · Mar 24, 2015

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.