I understand how to navigate between fragments using Jetpack's Navigation Component
, but I haven't been able to find is how to navigate from one child fragment to another child fragment.
The following is what I've done so far:
https://pastebin.com/dNQ0Ep4S the code example is pretty big so sum it up, I'm trying to do is" A>B and Ba>Bb". Ba
is a fragment inside of fragment B
. I'm not sure how to set up the nav graph
for something like that
The navigation works until the home
fragment. The bot nav
just doesn't seem to work. It always displays the pending layout. Another thing I tried was setting the nav graph
of the home
to the same one used for the login
and adding the pending and history fragments to the nav file without any actions. But the home
loads the login
, the tabs work, but they're replacing the home
fragment instead of being placed in the home nav host
.
Update
I managed to find the problem, but I'm stuck trying to find a solution.
So B
seems to be getting the navhost
for A
when I setup the botnavview
to the navcontroller
. So calling NavController navController = NavHostFragment.findNavController(this);
from B
returns the navhost
of A
. I'm at a lost here. If I do this NavController navController2 = Navigation.findNavController(currentActivity, R.id.homeNavHostFragmentContainer);
(homeNavHostFragmentContainer
being the navhost
for B
) still returns the navhost
for A
. For some reason, I can't get a reference to B
's navhost
.
My problem was that I misunderstood how NavHostFragment.findNavController()
functions. Fragment B
is not a navhost
is just a regular Fragment
. The navhost
for B
is a child of B
. The argument for findNavController()
has to be a NavHostFragment
not a regular Fragment
. So by passing, this
(being B
), it wasn't getting a NavFragment
, just a regular Fragment
. I thought the method would have extracted the navhost
from whatever fragment
was passed. Guess the documentation caused a bit of confusion. To get the correct navhost
, I had to get the navhost
from the fragment manager like so getChildFragmentManager().findFragmentById(navHostResId)
(in my case, navHostResId
was the id
of the fragment container in B
). Then everything worked beautifully.