I'm currently testing my app with a multipane Fragment
-ised view using the HC compatibility package, and having a lot of difficultly handling orientation changes.
My Host
activity has 2 panes in landscape (menuFrame
and contentFrame
), and only menuFrame
in portrait, to which appropriate fragments are loaded. If I have something in both panes, but then change the orientation to portrait I get a NPE as it tries to load views in the fragment which would be in the (non-existent) contentFrame
. Using the setRetainState()
method in the content fragment didn't work. How can I sort this out to prevent the system loading a fragment that won't be shown?
Many thanks!
Create new Instance only for First Time.
Here is the code snippet:
Save data on the fragment side
Here is the code snippet:
You can see the full working code HERE
It seems that the
onCreateViewMethod
was causing issues; it must return null if the container is null:Probably not the ideal answer but if you have
contentFrame
for portrait and in your activity only load up themenuFrame
when the savedInstanceState is null then your content frame fragments will be shown on an orientation change.Not ideal though as then if you hit the back button (as many times as necessary) then you'll never see the menu fragment as it wasn't loaded into
contentFrame
.It is a shame that the FragmentLayout API demos doesn't preserve the right fragment state across an orientation change. Regardless, having thought about this problem a fair bit, and tried out various things, I'm not sure that there is a straightforward answer. The best answer that I have come up with so far (not tested) is to have the same layout in portrait and landscape but hide the
menuFrame
when there is something in thedetailsFrame
. Similarly show it, and hideframeLayout
when the latter is empty.