Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar Tabs with fragments on rotate

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>
like image 948
Leandros Avatar asked Feb 07 '12 23:02

Leandros


1 Answers

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()

like image 94
Leandros Avatar answered Oct 31 '22 05:10

Leandros