Reputation: 7179
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?
Upvotes: 5
Views: 2579
Reputation: 67373
NavHostFragment
is basically creating a fragment with ChildFragmentManager
with it's own back stack which makes it much more easier to handle back stack.
If you ever tried, there is even one proper solution for handling back stack in nested fragments for a ChildFragmentManager
in Stackoverflow or i couldn't find, you have the pass initial parent fragment to children and remove transaction when the child fragment's onDestroy is called, etc, here is one link to check out it. I have seen even they used reflection to solve it.
In long story short, it's hard. NavHostFragment
makes it simple, NavController
also makes it easy to find start destination, current destination like properties.
You also need to have many NavHostFragments as much as you have tabs if you wish to have back navigation for each tab, you also need to use them with ViewPager2
to have nested navigation.
There is only one problem i see with NavHostFragment is you cannot create a NavHostFragment
with NavHostFragment.create
, here is my question, which is needed when you need one with custom properties such as ViewModel or DynamicNavHostFragment creation.
Also there is another issue with BottomNavigationView extension Google provided it leaks when BottomNavigationView itself is inside a fragment and that fragment gets replaced. I intend to write another extension for FragmentManager to solve issue.
I created a tutorial to play with NavHostFragments, NavController, ViewPager2, BottomNavigationView and memory leaks and how to overcome them. There are parts still i'm working on it yet, i'm currently fixing the documentation.
Upvotes: 0
Reputation: 471
I don't know if this helps, but I spent a few hours working on this. There wasn't much help back then.
How to use NavigationExtensions.kt in a Java project?
I'm using one Navhostfragment and multiple graphs for each tab I will have in the bottom navigation. If you need more clarity, I'm happy to help.
Upvotes: 0