Note that there are some explanatory texts on larger screens.

plurals
  1. POSwitching Fragments or Activities (ie layouts) on Orientation Change
    primarykey
    data
    text
    <p>I have a problem with switching activities &amp; fragments on certain device sizes, when an orientation change happens. In my case it's <code>large</code> screens, but it could happen with other screen sizes depending on the app. I've looked around for answers, but nothing seems to properly address this.</p> <p>I have two activities, MainActivity and SubordinateActivity. MainActivity is the only entry point to the app; MainActivity launches SubordinateActivity. Each activity has its own fragment, MainFragment and SubordinateFragment. When running on <code>normal</code> devices, there is only enough space on the screen for one fragment at a time, regardless of the orientation. In that case, each activity will manage its own fragment. On <code>xlarge</code> devices, there is enough space for two fragments, regardless of the orientation. In this case, there are different layout files which allow for two fragments on the screen. Both MainFragment and SubordinateFragment are managed by MainActivity (SubordinateActivity is never used).</p> <p>The trouble arises with <code>large</code> screens. Using landscape orientation, there's enough space for two fragments, but in portrait orientation there is not. I have the proper layout files for each. In landscape mode, MainActivity manages both fragments (as with <code>xlarge</code> devices) and in portrait mode, each activity manages its own fragment (as with <code>normal</code> devices). This produces problems in two scenarios:</p> <ol> <li>SubordinateActivity is loaded in portrait mode, and the orientation changes to landscape mode. <strong>What I want</strong>: SubordinateActivity should be discarded and MainActivity should load, with the content previously displayed by SubordinateActivity, displayed in its own SubordinateFragment. <strong>Problem</strong>: SubordinateActivity stays loaded by itself in landscape mode.</li> <li>MainActivity is loaded with MainFragment and SubordinateFragment in landscape mode, and the orientation switches to portrait. <strong>What I want</strong>: The content previously displayed in SubordinateFragment should now be displayed alone by SubordinateActivity. <strong>Problem</strong>: MainActivity is shown with only the content from MainFragment.</li> </ol> <p>A good example of this problem is the GMail app. Here are some screen shots from that app in case it's not clear what I'm talking about. I realize the UI of the GMail app is actually more complicated than mine, but the problem is the same.</p> <p><img src="https://i.stack.imgur.com/gPNYy.png" alt="GMail message list"> <img src="https://i.stack.imgur.com/Zu92E.png" alt="GMail message display"></p> <p><img src="https://i.stack.imgur.com/x6z11.png" alt="GMail in landscape orientation on large device"></p> <p>I'm sure that this is a problem others have encountered because the GMail developers encountered it too. I can't figure out what a <strong>good</strong> solution is, because every possibility seems to involve either violating the Android UI best practices, or creating some ungodly tangle between Activity code and XML layouts.</p> <p>Here are some ideas I have, none of which seems really correct:</p> <ul> <li>Detect the orientation change in both activities, and launch the other activity (eg, by using FLAG_ACTIVITY_CLEAR_TOP) to go back down the stack and load a previously loaded activity, with a new intent. This is a problem because the orientation change code should only be executed on <code>large</code> devices, which means mingling code that checks which layouts are available with the activity code.</li> <li>Drop SubordinateActivity altogether. It seems a bit superfluous and MainActivity can manage the fragments itself, even on <code>normal</code> size devices where it can just swap MainFragment and SubordinateFragment as necessary. In the end I don't think this solves the problem since MainActivity still relies on the layout files to tell it which and how many fragments to display. This also would violate the principle that an Activity represents a discrete thing that the user does.</li> </ul> <p>Here are some resources I've been looking at to try to solve this problem. As I said, this seems to be a common problem, but there seems to be no canonical Android solution. This is a bit of a shortcoming since the documentation recommends using Fragments, and every developer who does, is going to run into this problem.</p> <ul> <li><a href="http://developer.android.com/guide/components/tasks-and-back-stack.html" rel="nofollow noreferrer">Tasks and Back Stack</a></li> <li><a href="https://stackoverflow.com/questions/14142936/how-to-controll-android-back-stack">How to Controll Android back stack</a></li> <li><a href="https://stackoverflow.com/questions/10157643/how-to-start-a-new-activity-when-screen-orientation-changes-android">How to start a new activity when screen orientation changes? Android</a></li> <li><a href="https://stackoverflow.com/questions/8619883/onnewintent-lifecycle-and-registered-listeners">onNewIntent() lifecycle and registered listeners</a></li> <li><a href="http://developer.android.com/design/patterns/multi-pane-layouts.html" rel="nofollow noreferrer">Multi-pane Layouts</a></li> <li><a href="https://stackoverflow.com/questions/7604329/switch-from-dual-pane-to-single-pane-on-orientation-change-maintaining-fragment">Switch from dual pane to single pane on orientation change maintaining fragment stack</a></li> <li><a href="https://stackoverflow.com/questions/15814061/android-fragment-issue-with-orientation-change">android Fragment issue with orientation change</a></li> <li><a href="https://stackoverflow.com/questions/18138094/fragments-restore-state-on-orientation-changed">Fragments restore state on orientation changed</a></li> </ul> <p><strong>Summary</strong>: I have the multi-pane mode working for <code>xlarge</code> devices. The problem I'm trying to solve is switching between single pane (portrait) and multi-pane (landscape) mode on <code>large</code> devices that can only handle multiple panes in landscape orientation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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