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

2

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?

Squander answered 5/4, 2012 at 16:28 Comment(0)
A
2

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

Arezzo answered 6/4, 2012 at 15:21 Comment(3)
No, the unique line I added to the default manifest is 'android:screenOrientation="sensor"' for activitySquander
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?Arezzo
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.Squander
A
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); )

Arezzo answered 11/4, 2012 at 7:37 Comment(1)
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!Squander

© 2022 - 2024 — McMap. All rights reserved.