I have a BottomNavigationView with three types {Recipes Search Profile}. When you click on each of the menu items BottomNavigationView, the corresponding fragment appears. I want to achieve behavior so that when you press the Back button, the user gets to the previous fragment. Each fragment replaces the previous one in the following way:
beginTransaction() .replace(R.id.container, fragmentToSet, tag) .addToBackStack(tag) .commit();
It works well, with this example: Recipes -> Search -> Profile -> pressed Back -> Search -> pressed Back -> Recipes .
But the following problem arises. When you click on one of the menu items again, this transaction is pushed onto the stack and you get the following: Recipes -> Search -> Profile -> Recipes -> clicked Back -> Profile -> clicked Back -> Search -> clicked Back -> Recipes.
Recipes are entered twice. It is necessary that the first entry was removed from the stack, and the last recorded. How to implement it.