Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentManager popBackStack doesn't remove fragment

I'm implementing menu navigation using Fragments. So I begin with Home, and then users can navigate to diferent sections and details of each section.

When a user changes section, then I call pop on the fragmentmanager backstack until I reach Home, and then load the new section.

This is all working as expected. But I'm getting this problem:

  1. load a section that calls setHasOptionsMenu(true) on onResume()
  2. loads another section (old section it's suposed to get out of the stack). I see it OK. No menu is shown
  3. leave the application (for example, go to Android Laucher activity) and then when I return, I see the correct section, but it's showing the Menu of the old Fragment.

I've iterated the backstack and printed each fragment, and there it's not the fragment with the menu.

I put a debug mark on the onResume() method (where the setHasOptionsMenu(true) is flagged) and it indeed enters here, so the Fragment it's still somewhere.

I want to know if I'm doing something wrong and how could I solve it, thx

Update:

I'm using this code to load new fragments

fm.beginTransaction()
    .add(container, sectionFragment.getFragment())
    .addToBackStack(sectionFragment.getFragmentName())
    .commit();

And for remove:

private void clearStack(){
int count = fm.getBackStackEntryCount();
    while(count > 1){
        fm.popBackStack();
        count--;
    }
}

NOTE 1: I'm using add instead replace because I don't want to loose the state of my fragment when I navigate back from detail section. When I load another different section, then I call clearStack to pop the stack up to 1, and then loads new fragment. At the end, I'm calling executePendingTransactions() to finish to remove the fragments from the transaction.

NOTE 2: I'm seeing that it is entering on my fragment onDestroy() method, so it is suposed to be destroyed. But I don't know why it is getting called again when the Main activity resumes.

like image 948
edrian Avatar asked May 06 '14 15:05

edrian


2 Answers

I found that the problem was not in the logic of adding and removing fragment of the stack.

The problem was that some of the fragment loaded another fragments inside of it (it had ViewPager component). Then I thought that when the fragment was removed then these fragments were removed too.

This is true ONLY if you use getChildFragmentManager() method. This method MUST be used when loading fragments inside other fragmets. If not, then the fragments are asociated with the fragments activity.

like image 52
edrian Avatar answered Oct 12 '22 23:10

edrian


popBackStack will just revert your last FragmentTransaction.

If you use FragmentTransaction.add, popBackStack will just call FragmentTransacetion.remove.

But if you call FragmentTransaction.replace, popBackStack will call FragmentTransaction.remove and FragmentTransaction.add

For your "NOTE 1" : FragmentTransaction.replace will not change your fragment state.

like image 39
Neige Avatar answered Oct 12 '22 23:10

Neige