Different fragment wasn`t loaded when orientation changed (android)

Go To StackoverFlow.com

6

I created a test project that has two different fragments to be shown in the same activity. One fragment is for landscape and the other for 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 app on landscape the LandscapeFragment is shown, and when I open in Portrait the PortraitFragment is shown. So far, so good.

But if I open in landscape and rotate the device to portrait then the LandscapeFragment is reloaded and shown. Which is not the predicted behavior. The PortraitFragment should have been loaded.

And the same thing happens the other way around, if the device starts at the portrait orientation and then I rotate it to landscape: the PortraitFragment just gets reloaded, instead of having the LandscapeFragment loaded.

What am I doing wrong?

2012-04-05 16:28
by Luckcheese


2

Make sure that you don't have: android:config="orientation" in your manifest.xml

2012-04-06 15:21
by Nguyen NhatNam
No, the unique line I added to the default manifest is 'android:screenOrientation="sensor"' for activit - Luckcheese 2012-04-08 19:41
Do you try to make a break point in onCreate() of your activity to see whether this method is called when you rotate the device - Nguyen NhatNam 2012-04-08 20:32
Yes. It is called. I added log messages on onCreate for my activity and for each fragment. And when I rotate the device the onCreate of the wrong fragment is called after the activity`s onCreate - Luckcheese 2012-04-08 22:34


1

The problem is in your 2 layouts, the fragment ids are the same. Just change these fragment ids, like :

fragmentportrait for layout/main.xml

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

frag.setRetainInstance(false); )

2012-04-11 07:37
by Nguyen NhatNam
Ok, that worker. But I didn't undestand why it is needed to add different ids? Because on my code I have to test both to set frag.setRetainInstance(false).

But, I also accomplish to work making a unique layout with a FrameLayout and adding the fragments by code after testing the current orientation by getResource().getConfiguration().orientation - Luckcheese 2012-04-21 13:34

Ads