Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is possible to use only one Activity, but you'll have to manage the backstack when in portrait orientation yourself. </p> <p>As you describe the behaviour of the app, there could be 4 states:</p> <ul> <li>State A: portrait, showing the list fragment. </li> <li>State B: portrait, showing the details fragment with the list fragment on the back stack.</li> <li>State C: landscape, showing the list fragment on the left and a blank space on the right.</li> <li>State D: landscape, showing both the list fragment and the details fragment side by side.</li> </ul> <p>Imagine you are on state D (with both fragments, let's call them FA and FB), and you change the orientation of the phone. The following state should be the state B, but the problem is that your portrait layout only has one <code>FrameLayout</code>. Using the following as the single pane layout:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout android:id="@+id/list" ... /&gt; </code></pre> <p>Android will recreate FA and will have it attached to that <code>FrameLayout</code>. FB, on the other hand, will also be recreated but will not have a container associated, so it won't show up. At this point you could create another fragment, FB2 with the same contents as FB, and use it to replace FA. You will have FA-FB2 in your backstack and FB hidden. </p> <p>If you change the orientation again (expecting the state D), you can use <code>onSaveInstanceState()</code> to pop FB2 from the backstack with <code>popBackStack()</code>, thus having again FA and FB. Before removing FB2, you may also want to copy its content to FB. When the app is again in state D, FB will have its FrameLayout.</p> <p>There would be a few other transitions and situations that you would have to take care of, but the main idea would be that. </p> <p>It's not a very elegant solution, so I am sure that it has to be a better, maybe more efficient (without replicating Fragments for example) way to solve this problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload