I have a main Activity on which the ViewPager and NavigationTabBar with Fragment 1-3 are located. From these fragments, you can run more fragments (1.1-2.1), which completely close the previous fragments, but do not close the NavigationTabBar . In fragments 1.1-2.2 there is a button back, and you can also close them by pressing the hardware button back. This is implemented in the Yandex Market application. enter image description here

The question is how to close these fragments correctly. Simply deleting the last fragment from the stack ( popBackStack ) is not an option, since the user can first open Fragment1.1 in Fragment1, then scroll to Fragment2 there to open Fragment2.1, again scroll to Fragment1 and press the button back on the fragment 1.1 itself. Now the user sees Fragment1.1 and in theory he should close, but if you write like this:

 getFragmentManager().popBackStack(); 

then the last fragment will disappear, but I need a certain one.
If you write like this, the fragment will disappear, but it will not be removed from the stack:

 toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = getActivity().getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.remove(DonatFragment.this).commit(); } }); 

Tried to delete by name or id, did not work:

 DonatFragment hidenFrag = (DonatFragment) fragmentManager.findFragmentByTag(fragmentTag); fragmentManager.popBackStack(hidenFrag.getId(), 0); fragmentManager.popBackStack(fragmentTag, 0); 

    0