I have a parent fragment that holds a single child fragment, and both of the fragment\'s onAttach
and onCreateView
methods are logged for debugging
When you do a setRetainInstance(true), the fragment retains its configuration (and therefore its reference to the nested fragment, which is never really detached/attached). This can happen, for example, during a configurationChange (like a rotation).
So this is not a bug, it's by Android's design.
To answer your question, you could keep the Activity/Listener reference as a WeakReference so it gets released when it's no longer needed.
On the other hand, I'd rethink the whole idea, seems like you should have an observer pattern here (subscribe/unsubscribe from the listener/observer during onStop/onPause or similar).
note: that doesn't make it better, I loathe Android's lifecycle methods