Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments not visible after tab-switch in nested FragmentTabHost

I have a FragmentTabHost in the layout of one of my fragments (let's call it parentFragment). Now, I initialize each of the fragments that go in the tabhost programmatically in onCreateView of the parentFragment. When the parentFragment is created everything seems to work fine and it is showing the fragment of the first tab correctly.

But as soon as I switch the tab once, the fragments disappear (I can see the background color of the tabhost, but no content of the tabcontent-container). Also the first fragment is gone when I switch back to it. The fragments are actually being created as my logging shows, but they are not visible.

I assume it has to do with (re)creating the fragments, since I also tried using a ViewPager with a FragmentPagerAdapter. There I could happily switch between the first two fragments (preloaded due to setOffscreenPageLimit() ==1), but fragments (re)created beyond that were blank as well.

Any thoughts anyone??

Here is some of my code:

Extract of parentFragment's XML:

...
            <android.support.v4.app.FragmentTabHost
                android:id="@android:id/tabhost"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_alignParentTop="true" />

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="0dp"
                        android:layout_height="0dp" />

                    <FrameLayout
                        android:id="@+id/realtabcontent"
                        android:layout_below="@android:id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>
            </android.support.v4.app.FragmentTabHost>
...

Extract of parentFragment's XML:

...
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        ViewGroup layoutContainer = (ViewGroup) inflater.inflate(R.layout.fragment_app_details_test, container, false);

            tabHost = (FragmentTabHost) layoutContainer.findViewById(android.R.id.tabhost);
        tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB1"),
                FragmentSubTab1.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("TAB2"),
                FragmentSubTab2.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("TAB3"),
                FragmentSubTab3.class, null);

        return layoutContainer;
    }
...

Simple layout inflation in sub tab's fragment:

...
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup layoutContainer = (ViewGroup) inflater.inflate(R.layout.subfragment1, container,
                false);

        initUi(layoutContainer);
        return layoutContainer;
    }
...
like image 705
Till Avatar asked Nov 12 '22 15:11

Till


1 Answers

I haven't dealt with FragmentTabHost before but I would like to suggest you use the Action Bar method setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) to achieve tab navigation in your app (Doc here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs) This proposed method seems less work and uses fewer fragment holders. You basically instance your tabs, add them to the backstack and show/hide them according to the selected tab via onTabSelected or onTabUnselected methods.

Hope it helps.

like image 118
Andres Avatar answered Nov 15 '22 06:11

Andres