Search code examples
androidandroid-fragmentsandroid-architecture-navigationandroid-bottomnav

BottomNavigation in Fragment with navigation component


In my application I have a main screen with a BottomNavigation from Material design. From these screens I want to be able to start other Fragments where the BottomNavigation is hidden so that the new fragments fill the whole screen. Going back should then be possible via the back arrow. I guess you all can imagine this since it is a pretty common pattern today.

To implement this I want to use the navigation component with a single activity design. My thoughts were that my MainActivity just holds a NavHostFragment and a BottomNavFragment holds another NavHostFragment as well as the BottomNavigation.Because of the hiding possibility I cannot move the BottomNavigation to the MainActivity layout.

This seems a bit odd to me .Do I really need two navigation graphs and two NavHostFragments for this? The NavHostFragment in the BottomNavFragment only has the purpose to display the related fragments associated with the BottomNavigation. To navigate to other fragments I have to use the NavHostFragment from the Activity. If I use the same navigation graph it would collide with the start navigation and therefore an endless recursion.

Or should I use fullscreen DialogFragments for the other fragments I want to start? If so how can I tell the navigation component to start a dialog as fullscreen dialog? Is there a recommanded way to do this from Google?


Solution

  • Google recommends to listen NavController current destination using OnDestinationChangedListener and update ui as per the requirements. Look here