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.
<?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" /> <?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" /> <?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?
source share