I have a title bar (like a menu) and 4 fragments (MAIN, A, B, C), of which MAIN should be the "main / root" fragment for the backstack.
I have a problem when the user goes through the menu, for example, MAIN> A> B> C. If I just use backstack, it will go in the reverse order, which I do not want. I need a return button to return to MAIN no matter how the user navigated to one of these 3.

My current code (which is wrong, it closes the application if it is not in the MAIN, and the current fragment is switched from another non-MAIN fragment) looks like this:
private void SwitchFragment(Fragment pFragment) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.main_fl_fragmentcontainer, pFragment); if (_CurrentFragment == _Frag_Main) ft.addToBackStack(null); ft.commit(); _CurrentFragment = pFragment; }
android android-fragments navigation back-stack
Nerfpl
source share