4

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, it shows the list again.

Stefan
  • 5,203
  • 8
  • 27
  • 51
PJL
  • 18,735
  • 17
  • 71
  • 68

2 Answers2

1

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

AGrunewald
  • 1,735
  • 3
  • 16
  • 25
  • Thanks, I understand having different layouts for portrait and landscape and am finishing up a large project with a dual pane layout for both so am confident on what happens on an orientation change. The FragmentLayoutSupport example doesn't maintaing the data in the right pane on an orientation change, it reverts to the list. – PJL Oct 21 '11 at 07:41
0

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).

J.G.Sebring
  • 5,934
  • 1
  • 31
  • 42
  • Sorry, don't see how this answers the question. If in my dual pane layout I have a fragment stack of depth **N** say in the right pane then how do I maintain that on rotation which only has a single pane? – PJL Oct 26 '11 at 07:51
  • (single pane? : your question states both orientations have dual pane) Using the saved state or intent you can recreate the fragment stack. If the fragment order is dynamic you could save the fragments id comma separated to be able to recreate the order. – J.G.Sebring Oct 26 '11 at 12:57
  • Where exactly do I mention having dual panes for both in the question? See title. I'm suggesting that one solution is to have a dual pane for both and then hide the selection pane when in portrait. – PJL Oct 27 '11 at 10:55
  • Ok, sorry, I'm totally confused now in what you want to accomplish. My solution would handle both dual and single anyhow. You mention a large project with dual in both orientations in the question and in the comment on your answer from AGruneWald. – J.G.Sebring Oct 27 '11 at 18:44
  • Sorry if I was unclear. Currently I am coming off the back of a project which has a dual pane in landscape and portrait. I can envisage a time shortly though where I just need to support a single pane in portrait. So when this happens then in landscape if I have fragment A in right-pane then make a selection in A that replaces it with B then I would want on a rotation to show B in a single pane, i.e. the list fragment (left pane in landscape) has gone. Then on hitting back I want to show A and then hitting back again show the list. I don't belive your answer does answer that. – PJL Oct 27 '11 at 20:40