Currently I have a dual pane layout for both landscape and portrait on a tablet.
Similar to say settings I have a single activity which loads a fragment into the left selection pane and then choices made in whatever right fragment is being shown result in further fragments being displayed into the right pane adding to the backstack all the way.
Now I can see that for some tablets it would make sense to have a single pane for portrait and a dual one for landscape. However, I would want the following behaviour:
A) If in landscape a fragment stack of depth N say is in the right pane then on an orientation change to portrait I would still want fragment N to be displayed and the stack to be maintained
B) Then navigating back from fragment N in portrait mode I eventually want to come back to the selection screen.
Now I can't figure out how to achieve this, ideas that I've come up with thus far are:
1) Have dual panes for both orientations but hide the left one on orientation change, would then need to reshow and hide right pane when stack is cleared
2) Rebuild the stack into the new containter, adding the selection fragment at the beginning on a switch to portrait
Neither, if they work, seem entirely satisfactory to me. Just wondering what the better/recommended way of doing this is.
Thanks in advance. Peter.
Edit 1
It should be mentioned that I understand that you can have have different layouts for portrait and landscape and am finishing up a large project with a dual pane layout for both portrait and landscape so OK on the lifecycle of an orientation change. I have also looked at the FragmentLayoutSupport
example but that doesn't maintain the data in the right pane on an orientation change to portrait, 开发者_C百科it shows the list again.
PJL, so you are on the right track here, either you can check your orientation and then show or hide specific fragments or you can have two different layouts one for Portrait and one for Landscape, in your onCreate function of your Activity you can then check if the second Fragment is available via findViewById and then instantiate it or not. The View you are looking for would just have a FrameLayout with a known Id. So make sure you understand what happens during an orientation change enter link description here A really good Fragments Example is part of the Samples of the Book The Busy Coder's Guide To Advanced Android Development the book is part of a 40$ / year description. The Sample code is free though. Check out the Fragments Example it might help with your problems. Also of course you can have a look at the example FragmentLayoutSupport.java on developer.android.com
If you are using the list with action intents and uri's you can reorder the frags depending on orientation, action and uri in onCreate
.
If not I guess you can keep a selectedId value in preferences and use it to keep track of your detail-view.
You will have to keep this value up to date by saving it to preferences in onclick and make sure it is updated to -1 (or suitable) on things like back (if back closes this fragment).
精彩评论