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?
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.
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.
Google recommends to listen NavController
current destination using OnDestinationChangedListener
and update ui as per the requirements. Look here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With