onAttach() not called with setRetainInstance(true);

后端 未结 1 1481
情书的邮戳
情书的邮戳 2021-01-20 04:29

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

相关标签:
1条回答
  • 2021-01-20 05:09

    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

    0 讨论(0)
提交回复
热议问题