Fragments and Rotation

In the Gmail application, selecting an email from the left fragment opens the message in the right fragment as usual. Rotating the device to portrait only shows the selected email, as you would expect, and turning the device back into landscape view again displays the email list and the selected email address. This is all to be expected and works great.

However, in the API demos and in my application based on the fragment fragment, after selecting an element from the list of the left fragment and shown in the fragment of the right hand, turning the device to the portrait shows a fragment of the list instead of the selected element details the fragment, as in Gmail. Returning to the landscape again displays the selected item in the right fragment.

Demos API Code example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.html

Any idea how I can recreate what is shown in the Gmail app?

+3
source share
2 answers
John put me in the right direction with his answer. Indeed, in the portrait, XML only showed a list fragment:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.dixon.blah.Events$EventsListFragment"
        android:id="@+id/titles"
        android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

If I change this to a part fragment, however, I get FC, since no item has been selected from the list yet. I got around this by putting the code below in my FragmentActivity onCreate method:

if(detailsFrame != null) {
            Intent intent = new Intent();
            intent.setClass(this, EventDetailsActivity.class);
            intent.putExtra("index", mCurCheckPosition);
            intent.putExtra("id", mSelectedId);
            startActivity(intent);
}

, detailFrame . , , , .

+2

, /res/layout/fragment_layout.xml ( contentView Activity) , <fragment> .

, .

, , , , , .

( , , ), , Activity in , , , , ; . ... , , mIsItemSelected Activity, , / ( ).

+3

All Articles