I've got some problems understanding Fragments in Android.
The bottom navigation of my MainActivity
has got three items: FragmentA
, FragmentB
, FragmentC
.
FragmentC
has got a button. When the user clicks that button, another FragmentD
should display additional information.
FragmentD
should have an up-button in the toolbar. When the user clicks on that button, he gets back toFragmentC
.- When the user selects a different fragment from the navigation,
FragmentD
should disappear.
Inside the onClick of my button I've tried to use this
FragmentManager manager = requireActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, new FragmentD());
transaction.addToBackStack(null);
transaction.commit();
with nav_host_fragment
being the fragment container for the bottom navigation items of my MainActivity.
- Why doesn't
transaction.replace
replaceFragmentC
withFragmentD
? When I press the button,FragmentD
appears aboveFragmentC
and has got a transparent background.FragmentD
also does not get replaced by the other fragments when I select a different item from the bottom navigation. Is it because I'm using thenav_host_fragment
container forFragmentD
? - How do I get the up-button?