Android orientation change: different layouts, same fragments

This is pretty much a classic use case for Android.

Let's say we have 2 fragments: FragmentA and FragmentB.

In landscape mode, FragmentA and FragmentB sit side by side.

In portrait mode, each of them occupies the entire screen during use.

(see this image, but replace the tablet → landscape and phone → portrait) Different Layout, Same Fragments on Orientation Change

As explained here ( Support for single-panel and multi-level layouts ), there are two ways to achieve this:

1- Several fragments, one action: use one action regardless of the size of the device, but at runtime decide whether to combine fragments in the layout (to create a multi-panel structure) or exchange fragments (to create a separate design panel).

2- Several fragments, several actions: on the tablet, place several fragments in one type of activity; on the phone, use separate actions to place each fragment. For example, when a tablet design uses two fragments in an activity, use the same activity for mobile phones, but put an alternative layout that includes only the first fragment. When you start on the handset and you need to switch fragments (for example, when the user selects an item), start another action in which the second fragment is placed.

This makes great sense in theory, but I encounter some obstacles in trying to implement one of these approaches in such a way that restores the state of the fragments when changing orientation and pressing the button .

I tried the first approach and quite far, but found it useless because it required to manage all fragment transactions manually, in particular because the container with the fragment could not be easily changed. It is also painful to decide what to do next, because the last transaction on the back side may belong to a different orientation.

Now I am trying to use the second option. It still seems clean, but Fragments are recreated from scratch with every change of orientation (since each orientation uses a different action). I would like to have a way to restore the state of a fragment from another Activity / orientation.

Can someone explain how this can be done, or point me to the appropriate tutorial or sample application?

+6
source share
1 answer
  • add this configuration to MainActivity , where the Fragment replaces <activity android:name=".MainActivity" android:configChanges="orientation|screenSize|keyboard|keyboardHidden" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT"/></intent-filter> </activity
  • You create a landscape fragment layout for FragmentA and FragmentB enter image description here
  • if you need to change the behavior of both fragments, you must change the onConfigurationChanged fragment event.

@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { //write your stuff for landscape orientation }else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { //write your stuff for portrait orientation } }

+4
source

Source: https://habr.com/ru/post/988282/


All Articles