Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 508
Cilenco Avatar asked Apr 25 '20 09:04

Cilenco


People also ask

Where do you set the ID of a fragment to be used in navigation?

Where do you set the ID of a fragment to be used in navigation? In the fragment layout file, either by setting the ID attribute in the design editor or in the layout XML file in the res > layout folder. In the project navigation file, either by setting the ID attribute in the navigation graph or in the navigation.

How do you enable your project to use navigation components?

In the Project window, right-click on the res directory and select New > Android Resource File. The New Resource File dialog appears. Type a name in the File name field, such as "nav_graph". Select Navigation from the Resource type drop-down list, and then click OK.


Video Answer


1 Answers

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

like image 90
Askar Avatar answered Oct 24 '22 22:10

Askar