Luke
Luke

Reputation: 492

Fragments and Rotation

In the Gmail app, selecting an email from the left fragment opens the email in the right fragment as normal. Rotating the device to portrait shows only the selected email, as you would expect, and rotating the device back to landscape shows both the email list and selected email again. That is all expected and works perfectly.

However, in the API demos and in my application based on the Fragments sample after selecting an item from the left fragment list and it being shown in the right hand fragment, a rotation of the device to portrait shows the list fragment instead of the selected item details fragment like in Gmail. Going back to landscape reveals the selected item again in the righthand fragment.

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

Any idea how i can recreate that shown in the Gmail app?

Upvotes: 3

Views: 800

Answers (2)

Luke
Luke

Reputation: 492

Jon O pushed me in the right direction with his answer. Indeed on portrait, the XML only showed the 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 changed this to the details fragment however i get a FC as no item from the list has been selected yet. I got around this through placing the code below into 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);
}

Once an item in the list has been selected, the detailsFrame variable points to the View of the fragment. Thus from portrait rotation in the details fragment, to landscape and back again the View remains and is shown correctly.

Upvotes: 2

Jon O
Jon O

Reputation: 6591

I suspect that if you have a look in /res/layout/fragment_layout.xml (which is set as the contentView for the Activity) for the portrait mode, you'll find in the <fragment> tag a reference to the list fragment instead of the detail fragment.

If you switch this to reference the detail fragment instead of the list fragment, you'll be on the right track.

You may still have to dig around in the code to make the logic work out exactly as you want it to given the layout switch I just mentioned, but that's the place to start.

You'll run into another issue (which I haven't gotten around to thinking through just yet though I wanted to add this to my answer because I've got to run) which is that when you first launch the Activity in Landscape you'll likely want both fragments side-by-side; this would correspond to the list fragment in portrait. Selecting an item in the list would need to switch the expected portrait behavior to showing the other fragment... so perhaps keep a boolean mIsItemSelected and use it in your Activity constructor to decide which portrait layout/fragment pair you wish to display (the list or the detail).

Upvotes: 3

Related Questions