Different fragment did not load when changing orientation (android)

I created a test project that should be shown in the same action. One fragment for the landscape, and another for the portrait.

# My unique activity public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } # First Fragment public class LandscapeFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false); v.setText("LANDSCAPE"); return v; } } # Other Fragment public class PortraitFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false); v.setText("PORTRAIT"); return v; } } 

And I have two main.xml, one in layout / and the other in layout-land /. Each main.xml points to the correct fragment to be used.

 <!-- layout-land/main.xml --> <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment" android:name="br.luckcheese.test.LandscapeFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" /> <!-- layout/main.xml --> <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment" android:name="br.luckcheese.test.PortraitFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" /> <!-- layout/fragment.xml --> <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30sp" /> 

When I open the application on the landscape, LandscapeFragment is displayed, and when I open in Portrait, portrait fragmentation is displayed. So far so good.

But if I open the landscape and rotate the device to the portrait, then LandscapeFragment reboots and displays. This is not a predicted behavior. PortraitFragment must be uploaded.

And the same thing happens on the contrary, if the device starts with portrait orientation, and then I turn it to landscape: the portrait fragment simply reboots, instead of loading LandscapeFragment.

What am I doing wrong?

+6
source share
2 answers

Make sure you do not have: android:config="orientation" in your manifest.xml file

+2
source

The problem is in your 2 layouts, the fragment identifiers are the same. Just change these fragment identifiers, for example:

fragportrait for layout / main.xml

fragmentlanscape for layout-land / main.xml. (And change also: Fragment frag = getSupportFragmentManager (). FindFragmentById (R.id.fragment);

frag.setRetainInstance (false); )

+1
source

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


All Articles