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;
}
...