Search code examples
androidandroid-fragmentsandroid-actionbarandroid-tabs

Fragment disappears from backstack


Why does FragmentTwo disappear from the backstack in the following situation:

  • My app has a Fragment called FragmentOne in an Activity.
  • FragmentOne holds a Button. When clicked, it launches FragmentTwo, which is added to the Fragment backstack.
  • FragmentTwo has a Button, which when clicked adds two tabs to the ActionBar linked to two Fragments, FragmentThree and FragmentFour. FragmentThree is visible.
  • If I now click the back button, I expected to see FragmentTwo. Instead, I see FragmentOne. Where did FragmentTwo go?

Before I override onKeyDown() and start implementing my own backstack for Fragments I wanted to ask if there is something obvious I am missing? Note there is no configuration change happening when testing this.

enter link description here

Details:

FragmentOne's button click handler contains:

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    FragmentTwo fragment = new FragmentTwo();
    ft.addToBackStack(null);
    ft.replace(android.R.id.content, fragment).commit();

FragmentTwo button click is handled in the Activity:

    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    FragmentFour fragmentThree = new FragmentThree();
    FragmentFive fragmentFive = new FragmentFive();

    ActionBar.Tab tab = getActionBar().newTab().setText("Frag 3").setTabListener(new CustomTabListener<FragmentThree>(fragmentThree));
    getActionBar().addTab(tab);
    tab = getActionBar().newTab().setText("Frag 4").setTabListener(new CustomTabListener<FragmentFour>(fragmentFour));
    getActionBar().addTab(tab);

where the tab listener is:

public static class CustomTabListener<T extends Fragment> implements TabListener {
    Fragment fragment;

    public CustomTabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(android.R.id.content, fragment);
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}

I if add more Fragments to backstack before FragmentThree is shown, it is always and only the Fragment immediately before FragmentThree that has disappeared.

When I leave the Tabbed user view by pressing the back key and return to FragmentOne, the tabs are still showing. I understand I need to reset the ActionBar to NAVIGATION_MODE_STANDARD, but it's not clear why FragmentOne is showing instead of FragmentTwo.


Solution

  • I think the problem is if you build your tabs

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(android.R.id.content, fragment);
    }
    

    gets called and the fragment before is not added to the backstack

    did you try to call

    ft.addToBackStack(null);
    

    in the Fragment Two button handling code.

    But you need to implement onBackPressed() anyway to get rid of the tabs. I think I would make new activity with tabs.