9

I'am build an app with an ActionBar and two Tabs below. Everything works fine if the device / emulator isnt rotated. If rotated, tab state switches automaticale to tab1 (normal, because onCreate get called) but the content dont get changed. If I select a tab in the new orientation, the onCreateView() method from the selected Fragment get called but the view dont get updated (stay always the same). Any Tips?

The code.

Main Activity:

    ActionBar actionbar = getActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab ATab = actionbar.newTab().setText(R.string.player);
    ActionBar.Tab BTab = actionbar.newTab().setText(R.string.stations);

    Fragment AFragment = new AFragment();
    Fragment BFragment = new BFragment();

    PlayerTab.setTabListener(new MyTabsListener(AFragment));
    StationsTab.setTabListener(new MyTabsListener(BFragment));

    actionbar.addTab(ATab);
    actionbar.addTab(BTab);

With identical tabs that display a simple textview. The textview simple say which tab is selected.

Fragments:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.a, container, false);
}

The Fragment layout, mentioned above, only contains a TextView with hardcoded Text. (Only for testing purposes)


The Main layout looks like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 </LinearLayout>

</LinearLayout>
Leandros
  • 16,805
  • 9
  • 69
  • 108
  • Do you want onCreate() to be called when the device is rotated? – nhaarman Feb 08 '12 at 00:01
  • Yes and No, in this example is it ok. In final App, also Ok, but I think not the best way. Do you want use `android:configChanges="keyboardHidden|orientation"` ? I read about it and anyone said something about memory leaks and last resort. Is that right? Is there a better way? – Leandros Feb 08 '12 at 00:06

1 Answers1

19

Solved. I have recreated the fragment everytime, doesnt do that anymore solved it.

Changed in my TabListener and onTabSelected(Tab tab, FragmentTransaction ft), ft.add() to ft.replace()

Leandros
  • 16,805
  • 9
  • 69
  • 108
  • 2
    This bug is also in the official Android docs: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs Maybe someone can fix it there as well. – StefanMK Sep 10 '14 at 12:06