Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

I found this question, because after calling

fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

this code fragmentManager.getFragments().size() returns me the maximum number of fragments, which were in the stack. I checked every fragment on null. And I found that some fragment is null in my case. Maybe it will help someone)

If you are really looking to remove fragments at once then follow: How to replace Fragments of different types?

Otherwise use replace transaction for fragments to smooth transitiona and hassel free approach, see https://stackoverflow.com/a/23013075/3176433

Also understand Fragment lifecycle, http://developer.android.com/guide/components/fragments.html

I had a similar problem where the popBackStack() didn't remove my fragment. However, I noticed that I called the wrong FragmentManager, where I had to call getSupportFragmentMananger() instead of getFragmentManager().

Maybe there is a <fragment> or <androidx.fragment.app.FragmentContainerView> in an activity with android:name="androidx.navigation.fragment.NavHostFragment", app:defaultNavHost="true" and app:navGraph="@navigation/nav_graph".

In this case navigation is held by nav_graph. If you don't want to use NavController and NavHostFragment, maybe you should remove navigation and clean <fragment> tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top