Jdruwe
Jdruwe

Reputation: 3520

Android BackStack issue

I am having trouble fixing a problem that I have with the BackStack. I have 3 fragments that are managed by an Activity:

enter image description here

The navigation between the fragments I using this code:

private void showFragment(BabysitFragment babysitFragment, UserRequest userRequest) {
   FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
   switch (babysitFragment) {
      case CHILDREN_OVERVIEW:
        toolbar.setVisibility(View.VISIBLE);
        Fragment childrenSpecification = ChildrenOverviewFragment.newInstance(userRequest);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.fragment_container, childrenSpecification, BabysitFragment.CHILDREN_OVERVIEW.getSimpleName()).commit();
        break;
      case CHILD_SPECIFICATION:
        toolbar.setVisibility(View.VISIBLE);
        Fragment childSpecification = ChildSpecificationFragment.newInstance(userRequest);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.fragment_container, childSpecification, BabysitFragment.CHILD_SPECIFICATION.getSimpleName()).commit();
        break;
   }
}

//Gets called by FinalizeFrag and ChildSpecificationFrag
@Override
public void onChildrenOverviewNeeded(UserRequest userRequest) {
    showFragment(BabysitFragment.CHILDREN_OVERVIEW, userRequest);
}

//Gets called by ChildrenOverviewFrag
@Override
public void onChildSpecificationNeeded(UserRequest userRequest) {
    showFragment(BabysitFragment.CHILD_SPECIFICATION, userRequest);
}

I want the user to be able to go back to the FinalizeFrag using the backbutton from the other 2 fragments. This works with the current code but In 1 flow I do have a problem:

  1. The users saves the new 'Child' in the ChildSpecificationFrag (save icon in the toolbar)
  2. User gets redirected to the ChildrenOverviewFrag
  3. User presses back button
  4. Stays on the ChildrenOverviewFrag (SHOULD redirect to the FinalizeFrag)

NOTE: when I press the button again it does redirect to the FinalizeFrag

Code when clicking the close button (ChildSpecificationFragment):

if (id == android.R.id.home) {
  getActivity().onBackPressed();
}

Code when clicking the save button (ChildSpecificationFragment):

...
userRequest.getFamilyDetails().getChildren().add(child);
getActivity().getSupportFragmentManager().popBackStackImmediate();
onChildrenOverviewNeededListener.onChildrenOverviewNeeded(userRequest, this);

As you can see I am removing the Fragment from the BackStack. What am I doing wrong? Thanks in advance!

Upvotes: 1

Views: 245

Answers (2)

Green goblin
Green goblin

Reputation: 9996

Don't call addToBackStack, before doing "User gets redirected to the ChildrenOverviewFrag",

Upvotes: 2

sachithkn
sachithkn

Reputation: 467

i think

fragmentTransaction.addToBackStack(null); 

will add fragment to back stack.

public abstract boolean popBackStackImmediate (String name, int flags) 

just returns true if there was something popped, else false.

Upvotes: 0

Related Questions