I am having trouble fixing a problem that I have with the BackStack. I have 3 fragments that are managed by an Activity:
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:
- The users saves the new 'Child' in the ChildSpecificationFrag (save icon in the toolbar)
- User gets redirected to the ChildrenOverviewFrag
- User presses back button
- 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!