Amer Obeidah
Amer Obeidah

Reputation: 1

Android EventBus and Component LifeCycle

I am new to EventBus and was wondering what happens if the receiving party, an activity for example, gets destroyed or becomes null. Please I need an explanation for this as am trying to build standards for building (architecting) android application and EventBus is the way I am using to decouple the components.

Upvotes: 0

Views: 133

Answers (1)

s0nerik
s0nerik

Reputation: 691

You should register any receiver when it should start receiving events and unregister it from receiving events when it shouldn't (e.g. going to be destoyed). In case of Activity/Fragment you should consider lifecycle methods like onCreate/onDestroy and onResume/onPause as the main lifecycle method pairs.

If you won't unregister the receiver from receiving events when it's going to be destroyed, then, depending on the actual event bus you are using you will either waste some amount of memory (for holding a null reference in the receivers map) or get the NPE (if event bus implementation doesn't check the receiver for null).

Upvotes: 1

Related Questions