简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM