What exactly happens when Android recreates my app on an orientation change using nested fragments and getSupportFragmentManager()?

孤街浪徒 提交于 2019-12-02 18:06:28

问题


I've looked at other threads related to this issue and they all seem to use getChildFragmentManager() but if possible, I'd like to use getSupportFragmentManager().

Here is the issue: I have parent fragment A which holds child fragment B. B is a selector fragment. When the user makes a selection, B is "replaced" with C. So now A holds C. The user can also go back to B and open up a C displaying some other data, we'll call it C' . When the user goes back it is a "replace" transaction (not back stack). Also, C and C' are different instances of the same fragment class.

This for the most part works fine but I found a scenario that causes abnormal behavior.

  1. User starts in portrait, B displaying

  2. User in portrait, selects C

  3. User switches to landscape

  4. User in landscape, C displays

  5. User switches back to portrait

  6. User in portrait, C displays

  7. User in portrait, returns to B

  8. User in protrait, selects C'

  9. User switches to landscape

  10. User in landscape, C' displays

  11. User switches back to portrait

  12. User in portrait, B displays Huh?!

At #12, C' should still be displaying, not B (the selector fragment). Why didn't Android remember C' here? What exactly happens when Android recreates my app on an orientation change using nested fragments and getSupportFragmentManager()?

I have a thought that it may have something to do with the difference between referencing a fragment managed by getSupportFragmentManager() using a container Id vs a tag. If I use a container Id, could this be getting screwed up on orientation changes because xml files are being swapped (even though the container id name doesnt change). Hopefully that makes sense. I am using container Id's btw.

Some other background info: I'm not using the back stack at all in this specific situation. When I change fragments, I am using "replace" transactions and the "new" reserved word and creating a new nested fragment.

Also, here is an image to help you better understand what A, B, C, C' is:


回答1:


The issue had to do with the fact that I was calling super.onCreate(savedInstanceState) which maintains old fragments on an orientation change and creating new fragments at the same time. Didn't realize super.onCreate(savedInstanceState) did that.



来源:https://stackoverflow.com/questions/36091727/what-exactly-happens-when-android-recreates-my-app-on-an-orientation-change-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!