1

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!

Jdruwe
  • 3,450
  • 6
  • 36
  • 57

2 Answers2

2

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

Green goblin
  • 9,898
  • 13
  • 71
  • 100
  • In the case of CHILDREN_OVERVIEW or CHILD_SPECIFICATION? Can you also explain why? Thanks! – Jdruwe Jul 04 '15 at 18:36
  • Try both. Please let me know if it works. I had used it long back and it did work for me. – Green goblin Jul 04 '15 at 18:38
  • I've replaced it with 'fragmentTransaction.addToBackStack("scheduleFragment");' in both cases but I still need to click back 2 times in the ChildrenOverviewFrag to go back to the FinalizeFrag. – Jdruwe Jul 04 '15 at 18:41
  • It seems like `onbackstackchangedlistener` can solve your issue – Green goblin Jul 04 '15 at 18:44
  • And how does it help? – Jdruwe Jul 04 '15 at 18:45
  • BTW, where are you committing the fragment transaction? – Green goblin Jul 04 '15 at 18:48
  • On the end of the fragmentTransaction.replace(...).commit() line. – Jdruwe Jul 04 '15 at 18:49
  • I see. Before doing "User gets redirected to the ChildrenOverviewFrag", don't call addToBackStack – Green goblin Jul 04 '15 at 18:50
  • This seems to work but now The content of the ChildrenOverviewFrag shows under the FinalizeFrag like so: http://imgur.com/yZlpFC6. Do I have to remove the ChildrenOverviewFrag in some way? – Jdruwe Jul 04 '15 at 19:02
  • Yes. You need to override `onbackstackchangedlistener` and when you return to FinalyzeFrag, you need to remove contents of ChildrenOverviewFrag – Green goblin Jul 04 '15 at 19:07
  • I am still not sure how you want the onbackstackchangedlistener to look like? Can you maybe give a code example? – Jdruwe Jul 04 '15 at 19:08
  • See it: http://stackoverflow.com/questions/23599519/android-onbackstackchanged-not-called – Green goblin Jul 04 '15 at 19:13
  • How do I know that I am going from the ChildrenOverviewFrag to the FinalyzeFrag? Sorry for all these questions :S – Jdruwe Jul 04 '15 at 19:14
  • I think `getBackStackEntryCount()` can be used to determine where are you. When you return to `finalyzeFrag`, can back stack entry count be used here? – Green goblin Jul 04 '15 at 19:20
  • You can also handle things in `onResume` of finalyzeFrag. See here on how to force call `onResume` of a fragment: http://stackoverflow.com/questions/6503189/fragments-onresume-from-back-stack – Green goblin Jul 04 '15 at 19:24
  • The count it 1 in both situations: from finalize to overview, from specification to overview. So it seems impossible for me to know when to clear. – Jdruwe Jul 04 '15 at 19:28
  • Sigh! I can think of one more approach, though it is kinda hack. Use `SharedPreferences` and write a value to it whenever user saves a child. In this way, you know that when to handle it after checking content of `SharedPreferences`. Once you handle it, clean the value of `SharedPreferences` so that it can be used next time user saves a new child – Green goblin Jul 04 '15 at 19:32
  • And when do you suppose I read from the preferences and clear the container? – Jdruwe Jul 04 '15 at 19:36
  • Inside onBackStackChanged method – Green goblin Jul 04 '15 at 19:37
  • I am going to try some stuff, I suppose I also have to check that I am currently displaying the FinalizeFrag. – Jdruwe Jul 04 '15 at 19:43
  • Let me know if you get stuck into any issue – Green goblin Jul 04 '15 at 19:44
  • I will, thanks for the help! The SharedPreferences solution seems a bit dodgy but I am looking into it. – Jdruwe Jul 04 '15 at 19:45
0

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.

sachithkn
  • 457
  • 1
  • 4
  • 13
  • I am not sure what you mean, this is what I found on the docs: Like popBackStack(), but performs the operation immediately inside of the call. This is like calling executePendingTransactions() afterwards. Returns true if there was something popped, else false. It does seem to me that It pops the latest added item. – Jdruwe Jul 04 '15 at 18:32