In Fragment examples involving callbacks, usually they assign the listener in the onAttach method and then set the listener to null in the onDetach method.
Is this l
I have never explicitly set listener references to null
, since in most cases it won't make a difference.
Here are some clarifications on your other questions:
onDetach()
and onDestroy()
reflect the component lifecycle, but not the object lifecycle. Nothing automatic will happen there. When your fragment instance is garbage collected, the reference to the listener will be destroyed with it. If this is the only reference to your listener, it will be also eligible for garbage collection.onAttach()
, so you are sure that you can delegate events properly. After onDetach()
, you won't receive any events that will require delegation, until you re - attach the fragment. And, if you re - attach it, then you will have the correct listener instance, since you have already taken care of that in onAttach()
.