Fragment transactions, actionbar & backstack

Viewed 4941

In my main activity, I have an actionbar with NAVIGATION_MODE_TABS. The contents of each tab is a listfragment.

I'd like to make it so that that when a listitem is clicked, a new fragment is brought into view, and the actionbar mode is changed to NAVIGATION_MODE_STANDARD (so that the tabs are now hidden).

I've managed to get this to work with the following code:

In the listitemclick method:

        ActionBar actionBar = getActivity().getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

        Fragment albumListFragment = new AlbumListFragment();
        albumListFragment.setArguments(bundle);
        FragmentTransaction ft = getFragmentManager().beginTransaction();

        ft.replace(android.R.id.content, albumListFragment);
        ft.addToBackStack(null);

        // Commit the transaction
        ft.commit();

        Log.i("FragmentList", "Item clicked: " + id);

The problem is when I press the back button, the tabs are still gone, and the previous fragment doesn't come back into view.

Am I doing something wrong.. Is it something to do with the fragment backstack? Should I go about it another way, or even override the backpress?

--Edit--

For clarity - I am calling addToBackStack when I call fragmenttransaction.replace, but when I press the back button, the previous fragment is not restored.

4 Answers
Related