Fragment within FragmentTabHost view lost onBackPressed Android?

470 Views Asked by At

I have created a application. Following is scenario before I explain problem.

I have Activity A which has multiple fragments such as F1,F2,F3 etc.

Now for F1 fragment I have implemented FragmentTabHost with three Fragments F11,F12,F13 fragment views. On tab is working fine for this.

But today i noticed one problem. Say I am inside F1 I show three fragment tabs F11,F12,F13. User can switch between tabs and it works fine.

Problem is say i goto Fragment F13 from F11 by pressing tab. It shows F13 fragment successfully.

However when I click Back Button on menu it goes back F11 fragment but empty screen is shown means F11 view is not shown..

This is my F1 fragment code implementing FragmentTabHost:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.home_fragment, container,
                false);
        setHasOptionsMenu(true);
        // realtabcontent = (FrameLayout) rootView
        // .findViewById(R.id.realtabcontent);
        mTabHost = (FragmentTabHost) rootView
                .findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(),
                R.layout.home_fragment);
        mTabHost.setOnTabChangedListener(this);
        View tabView = createTabView(getActivity(), "Featured");
        spec = mTabHost.newTabSpec("featured").setIndicator(tabView);

        mTabHost.addTab(spec, FeaturedHomeTab.class, null);

        tabView = createTabView(getActivity(), "Top");
        spec = mTabHost.newTabSpec("top").setIndicator(tabView);
        mTabHost.addTab(spec, TopHomeTab.class, null);

        tabView = createTabView(getActivity(), "New");
        spec = mTabHost.newTabSpec("new").setIndicator(tabView);
        mTabHost.addTab(spec, NewHomeTab.class, null);
        onTabChanged("featured");
        return rootView;
    }

So this is main code with three fragments and when i back from one fragment to previous fragment in tabhost view disappears.

What can be problem. Please help.

1

There are 1 best solutions below

0
On

Tabs aren't meant to participate in temporal navigation (which is what back nav is for) because they represent content at same level of hierarchy.

In case of fragments, the back nav usually pops the back stack. There is a caveat that back nav doesn't pops sub (child) fragments first. So, the fragments added to Activity are removed on back, this includes the entire tabs fragment (along with its child fragments).