Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 919
levavare Avatar asked Mar 24 '15 15:03

levavare


People also ask

Which activity lifecycle method would you use?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .

What is the activity lifecycle callback method for an activity that is no longer visible to the user?

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.

Which lifecycle method is called to give focus to an activity?

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.


1 Answers

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.

like image 50
Amit K. Saha Avatar answered Jan 21 '23 21:01

Amit K. Saha