7

I want to be able to reverse a replace FragmentTransaction by using addToBackStack():

FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();         
Fragment scheduleFragment = new ScheduleFragment();     
fragmentTransaction.replace(R.id.content_container, scheduleFragment, "scheduleFragment");
fragmentTransaction.addToBackStack("scheduleFragment");
fragmentTransaction.commit();

but after that, clicking the back button does nothing.

From the doc and it's supposed to reverse the transaction.

What am I missing?

Charles
  • 50,943
  • 13
  • 104
  • 142
jul
  • 36,404
  • 64
  • 191
  • 318
  • 2
    The [doc](http://developer.android.com/training/implementing-navigation/temporal.html) (see "Implement Back Navigation for Fragments") says to use `.add` instead of `.replace`. Check the answers here: http://stackoverflow.com/questions/18634207/difference-between-add-replace-and-addtobackstack but I guess `.replace` is not fully supported while `.add` seems to work correctly. – electrocrat May 25 '15 at 03:35
  • @Abel Paz' answer below is the right fix for the issue with `.replace` – electrocrat May 25 '15 at 03:44

4 Answers4

25

The right way to do this is using onBackPressed() method to catch that back event in your app, and then "pop" the backStack with popBackStack(). For example:

public void onBackPressed()
{
    // Catch back action and pops from backstack
    // (if you called previously to addToBackStack() in your transaction)
    if (getSupportFragmentManager().getBackStackEntryCount() > 0){
        getSupportFragmentManager().popBackStack();
    }
    // Default action on back pressed
    else super.onBackPressed();
}

PD: Sorry for the delay answering, but I just saw your question. Hope it helps!

Abel Paz
  • 573
  • 7
  • 20
  • Then why the hell, in Android documentation, they communicated the wrong way. :( – Kamalakannan J Mar 13 '15 at 14:59
  • 2
    I don't think this logic is necessary. App will function exactly the same if you use `addToBackStack`. – IgorGanapolsky Apr 24 '15 at 15:55
  • Contrary to the way all the doco seems to point to the usage of these constructs, I too am only able to get this working as intended w/ the addition of this code in `OnBackPressed` :/ – bc3tech Jan 20 '16 at 21:16
  • Furthermore, it appears this means this part of the Android doco is flat out wrong http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments - "When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it)." – bc3tech Jan 20 '16 at 21:23
  • if you have overridden onBackPressed() in your activity FragmentTransaction won't work out of the box. Hence, will be checking for the backstack count and popping the backstack manually. – JItesh SUvarna Jun 21 '17 at 14:01
4

Try fragmentTransaction.addToBackStack(null) The parameter for addToBackStack() is an optional name for the back state, you do not use the tag in the replace() method which is just an optional tag for the fragment. You can read more about this here.

Ryan Smith
  • 1,628
  • 3
  • 13
  • 18
  • Yes you can keep the tag for referencing the fragment, but does using `addToBackStack(null)` work? What happens if you use this? – Ryan Smith Aug 19 '13 at 04:07
  • I haven't tried because it does not make sense to me. So I implemented my own `Fragment` stack (see my answer). – jul Aug 19 '13 at 04:29
  • 2
    Your answer works but I believe it is unnecessary. When you use `addToBackStack()` you do not pass the fragment or its tag, you pass the name of the stack for it to be added to. When you look at the documentation it says "name An optional name for this back stack state, or null" where name is the string parameter. I think that you could just use `addToBackStack(null)`, it should at least be worth a try. – Ryan Smith Aug 19 '13 at 05:13
2

I's been a while but I hope this will help someone.

fragmentTransaction.addToBackStack(null) won't work if you are extending AppCompatActivity. It works well in Activity. I couldn't find the reason.

Nima
  • 112
  • 1
  • 2
  • 12
0

I implemented my own Fragment stack:

public Fragment addFragmentToStack(Fragment fragment){
    if(MyApplication.fragmentStack.size()>20){
        MyApplication.fragmentStack.remove(0);
    }
    return MyApplication.fragmentStack.push(fragment);
}   

public void onBackPressed() {
        if (MyApplication.fragmentStack.size() > 0) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_container, MyApplication.fragmentStack.pop());
            ft.commit();
        }
        else{
            finish();
        }
}
jul
  • 36,404
  • 64
  • 191
  • 318