简体   繁体   中英

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.

You should register any receiver when it should start receiving events and unregister it from receiving events when it shouldn't (eg 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM