I have a problem with switching actions and fragments on certain device sizes when the orientation changes. In my case, these are large screens, but this may happen with different screen sizes depending on the application. I was looking for answers to questions, but nothing seems to be suitable for this.
I have two actions: MainActivity and SubordinateActivity. MainActivity - the only entry point to the application; MainActivity launches SubordinateActivity. Each action has its own fragment, MainFragment and SubordinateFragment. When working on normal devices, the screen has only enough space for one fragment at a time, regardless of orientation. In this case, each action will control its own fragment. On xlarge devices, there is enough space for two fragments, regardless of orientation. In this case, there are different layout files that allow you to display two fragments on the screen. Both MainFragment and SubordinateFragment are controlled by MainActivity (SubordinateActivity is never used).
The problem occurs when using large screens. Using landscape orientation, there is enough space for two fragments, but not in portrait orientation. I have corresponding layout files for each. In landscape mode, MainActivity manages both fragments (both with xlarge devices), and in portrait mode, each action controls its own fragment (as with normal devices). This creates problems in two scenarios:
- SubordinateActivity loads in portrait mode, and the orientation changes to landscape mode. What I need : SubordinateActivity should be canceled, and MainActivity should load, with the content previously displayed by SubordinateActivity displayed in its own SubordinateFragment. Problem : SubordinateActivity remains loaded by itself in landscape mode.
- MainActivity is loaded with MainFragment and SubordinateFragment in landscape mode, and the orientation switches to portrait. What i want . Content previously displayed in a SubordinateFragment should now display on its own SubordinateActivity. Problem : MainActivity is displayed only with MainFragment content.
A good example of this problem is the GMail application. Here are some screenshots from this application, if it is not clear what I'm talking about. I understand that the user interface of the GMail application is actually more complex than mine, but the problem is the same.



I am sure this is a problem that others have encountered because GMail developers have encountered this. I canโt understand what a good solution is, because every possibility seems to be related either to a violation of the best practices of the Android UI, or to the creation of some unholy interweaving between the operation code and XML layouts.
Here are some ideas that I have, none of which seem really correct:
- Determine the orientation change in both actions and run another action (for example, using FLAG_ACTIVITY_CLEAR_TOP) to return to the stack and load the previously loaded activity with a new intention. This is a problem because the orientation code should only run on
large devices, which means mixed code that checks which layouts are available with the activity code. - Drop SubordinateActivity altogether. It seems a little redundant, and MainActivity can manage the fragments themselves, even on
normal sized devices, where it can simply swap MainFragment and SubordinateFragment as needed. In the end, I don't think this solves the problem, since MainActivity still relies on layout files to tell which and how many fragments to display. It also violates the principle that an action is a discrete thing that the user performs.
Here are some resources that I tried to try to solve this problem. As I said, this seems to be a common problem, but there seems to be no canonical solution for Android. This is a bit of a drawback, as the documentation recommends using Fragments, and every developer who does this will run into this problem.
- Tasks and Back Stack
- How to control Android background stack
- How to start a new activity when changing the screen orientation? Android
- onNewIntent () life cycle and registered listeners
- Layered Layouts
- Switching from a dual panel to a separate orientation panel supporting a fragment fragment
- Problem with android fragments with orientation change
- Changed fragment recovery state when changing
Summary I have multi-user mode for xlarge devices. The problem I'm trying to solve is switching between single (portrait) and multi-level (landscape) modes on large devices that can handle only a few panoramas in landscape orientation.