androidandroid-fragmentsandroid-tabhost

android: tabhost in a fragment, w/ fragments as tabs


I am trying to have a tabhost that it contained in a fragment, where the tab content is defined by fragments. okay then,

I started from the FragmentsTab example from google. this shows how to use fragments as the tab content, but the TabHost itself is still hosted in an activity. again, i need the tab host to be hosted in a Fragment.

I took FragmentsTab example, and moved the the code in onCreate() and moved it to onCreateView() in my Fragment,

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

    mTabHost = (TabHost) fragmentView.findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mTabManager = new TabManager(getActivity(), mTabHost, R.id.realtabcontent);

    // TODO: localize
    mTabManager.addTab(mTabHost.newTabSpec("info").setIndicator("File Info"), InfoFragment.class, null);

    return fragmentView;
}

when i run, i get this exception,

05-25 14:42:27.272: E/AndroidRuntime(4701): Caused by: java.lang.IllegalStateException: Recursive entry to executePendingTransactions
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1343)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:429)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.onTabChanged(TabManager.java:116)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.setCurrentTab(TabHost.java:344)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.addTab(TabHost.java:216)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.addTab(TabManager.java:92)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.DetailFragment.onCreateView(DetailFragment.java:46)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:819)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1371)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:429)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.onTabChanged(TabManager.java:116)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.setCurrentTab(TabHost.java:344)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.addTab(TabHost.java:216)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.addTab(TabManager.java:92)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.DetailFragment.onCreateView(DetailFragment.java:46)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:819)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1111)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

looking at the trace, i see onCreateView() for DetailFragment (the fragment hosting the TabHost) call recursively. somehow, the call to TabManager.addTab() (again see the FragmentsTab example) is resulting in a recursive call to DetailFragment.onCreateView().

What am I doing wrong? Is this even possible?


Solution

  • Android doesn't allow you to embed one fragments inside other fragments. Your logcat indicates that you are executing one fragment transaction within another (i.e. recursive entry to executePendingTransactions). It's just not allowed, sorry!