I have a HomeFragment1
, ClassFragment2
, QuestionFragment3
, LastFragment4
. The user will navigate in the order mentioned. I want to return to HomeFragment1 from anywhere the user presses back button.
I tried removing addToBackStack in all fragments except HomeFragment1 but when pressing back button the fragments overlap.
I wrote in the onBackPressed() of the MainActivity as below:
if(getFragmentManager().getBackStackEntryCount() > 0 ) {
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
super.onBackPressed();
}
But it returns from 4 to 3 to 2 to 1 in this order for each back press. I would like to go directly to 1 from any fragment.