1

I've been trying to figure this one out.

I'll start by saying that there are many StackOverflow solutions but most say to hookup the onBackPressed() myself, which does work, but I don't understand why I don't get that behavior for free with the .addToBackStack

Can't find anything relevant in the documentation except that it should have worked.

I am using the simplest of forms to add a fragment to the backstack

getActivity().getSupportFragmentManager().beginTransaction().add(R.id.create_fragment_holder2, new MyFragment(), TAG).addToBackStack(TAG).commit();

getActivity is a FragmentActivity. Goes along with this FrameLayout:

 <FrameLayout
        android:id="@+id/create_card_fragment_holder2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

And a simple fragment with a TextView with a blue background

public class MyFragment extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.my_layout, container, false);
}}

Problem is that if I don't ask .addToBackStack then the back button will close the containing fragment, and if I do, the back button is non responsive and I will forever see that blue screen.

Every StackOverflow solution I found says to hookup the onBackPressed() myself, which does work, but I don't understand why I don't get that behavior for free with the .addToBackStack Can't find anything relevant in the documentation except that it should have worked.

UPDATE: I found out it was not working because I was blocking it in the onBackPress in the activity. So without it it will work as expected. That said it's a good place to enter validation and prevent it from working, should that be your need :)

Alon
  • 601
  • 9
  • 19

2 Answers2

1

Override onBackPressed() into your activity and call this in order to remove current fragment from backstack, since you add it.

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
    getSupportFragmentManager().popBackStackImmediate()
} else {
    finish();
}

This will work only when you add fragment to backstack using addToBackStack() method.

When you add a fragment to backstack to keep tracking your back flow and all the previous changes, that instance will be keeped into FragmentManager. When you want to go back to previous fragment, just pop the latest fragment from backstack. If you don't add it to stack, you will not be able to roll back the taken path and all the previous oprations.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
0

It looks like you know how to get around the issue you're running into, and your post seems to be asking one specific question, so I'll answer that:

I don't understand why I don't get that behavior for free with the .addToBackStack

It does! But only to a point. The back button will automatically navigate back through the back stack, but it will not close the last fragment, if there is nothing to navigate back to. So, if you were to add another fragment to the back stack (and not override the back button), it would automatically navigate the user back to your first fragment, but then do nothing after that.

WoogieNoogie
  • 1,258
  • 1
  • 11
  • 21