-1

I have registered event bus in one activity like - EventBus.getDefault.register(this);

But Now i want to unregister it in to another activity.So how to achieve it?

sanil
  • 482
  • 1
  • 7
  • 23
  • Possible duplicate of [Is an event bus unregister necessary](http://stackoverflow.com/questions/30486497/is-an-event-bus-unregister-necessary) – Kewin Dousse Apr 06 '17 at 07:42
  • You can do a unregister with the same registered activity in a different activity if you have the reference to the registered activity. But it is recommended to unregister the activity on the onPause state . otherwise it will give memory leaks . – Krish Apr 06 '17 at 07:51
  • @Krish can you please give me a short example? – sanil Apr 06 '17 at 08:17
  • post some code where you want to unregister. – Krish Apr 06 '17 at 08:20

1 Answers1

1

But Now i want to unregister it in to another activity.

You cannot unregister another activity because it is not registerd. You must unregister the same object you registered. Usually onResume()/onPause() are good place to deal with it.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • but when i remove my application from background then what should i do to unregister the eventbus of that respective activity as i am register it in to onStart() and unregister it in onDestroy() method? – sanil Apr 06 '17 at 08:03
  • this sounds like bad design. what you need your activity registered when it is in background?! User may never return to it, so there's no point. – Marcin Orlowski Apr 06 '17 at 08:17
  • No,i had created one Subscriber method in launcher activity which i need to be called in others activity by Event posting,that's why i unregister event bus in onDestroy() instead of OnPause().But when application gets removed from background then how should i unregister that event bus? – sanil Apr 06 '17 at 08:28
  • @sanil You dont have to unregister the activity if the user kills the apps. It is not a good idea to unregister it in onDestroy. – Krish Apr 06 '17 at 08:39