问题
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.
User starts in portrait, B displaying
User in portrait, selects C
User switches to landscape
User in landscape, C displays
User switches back to portrait
User in portrait, C displays
User in portrait, returns to B
User in protrait, selects C'
User switches to landscape
User in landscape, C' displays
User switches back to portrait
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