I have a main activity that implements the bottom navigation view with 5 fragments on it. For example, when I press the search button in the first fragment and then fragment search was displayed. But when I go to another bottom navigation view (fragment c) and pressing back on the phone, it causing overlapping between fragment A and fragment C.
This is the code between fragment A and fragment search
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
searchHerbs searchHerbs = new searchHerbs();
searchHerbs.setArguments(arguments);
ft.replace(R.id.main_frame, searchHerbs);
ft.addToBackStack(null);
ft.commit();
this is the code of fragment transaction between bottom navigation view fragment
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame,fragment);
fragmentTransaction.commit();
If I trace the code. the problem is : 1. There is no addbackstack when I leave the fragment search, I want to add it in onDestroyView but I don't know how. 2. I went to fragment search from fragment A using addbackstack(null). 3. when I press another fragment from bottom navigation view (fragment c) and press back. it causing pop fragment to fragment A.
I already tried with adding addbackstack(null) in the fragment transaction on the bottom navigation view. The problem is solved, but it makes another problem like : 1. it causing blank fragment when I press back stack before the apps close. 2. active bottom navigation doesn't change when I press back even though the fragment was changed.