Hello! Anyone knows if it's possible to use Scaffo...
# compose
l
Hello! Anyone knows if it's possible to use Scaffold with a BottomNavigation only on certain screens using navigation graphs (loginGraph, mainGraph, etc using nested navigation), I know that we can't use Composables on NavHost() but there is another way? In this way we can encapsulate the Scaffold only on parts of the app that needs BottomNavigation and we can control this state separately
f
Related to https://github.com/google/accompanist/issues/729. Unfortunately, the issue was auto-closed.
l
Nice, I'll try to add some interest on the issue. How did you solve this problem with two NavHosts()?
f
In the case of the example app shown in the issue, I used an upper and a lower
NavHost
. The upper
NavHost
contains two routes:
details
and
lower
. The lower NavHost also contains two routes:
gallery
and
folders
. Each NavHost gets its own
NavController
. The
lower
route contains a
Scaffold
wrapping the lower
NavHost
. Then I can use the lower
NavController
for interactions with the scaffold and the upper
NavHost
when navigating to
details
and back.
This is a solution, but gets pretty messy with more screens, so a alternative solution would be great. Especially using different `NavController`s for different navigations is pretty bad imo.
l
yeah, how do you manage to navigate to the
lower
NavHost from the upper?
and can you share your NavGraph with the two NavHosts? I didn't find an example on the docs for this situation
f
As far as I'm concerned, you can't share you NavGraph, so you need to NavControllers.
l
sorry, I was not clear hehe I meant, can you share your Navigation code?
f
To navigate to the lower NavHost, I do something like
upperNavController.navigate("lower")
or
upperNavController.popBackStack()
. In my use case, this is enough, but further navigation in the lower nav controller might be required.
Sorry, I don't quite understand what code you mean. For me, there were no code duplications.
l
Sorry, I mean, can you share your Navigation code, I didn't find an example on the Android docs with two NavHost()
Thank you for the explanation on how to navigate between upper and lower 🙂
f
Oh, sorry. Yeah, there is no example, since having two NavHosts is considered bad 😄 I don't have the code for the sample app anymore, so I can't share it right now.
i
Is there a reason you aren't using the
visibleEntries
property on NavController to coordinate the showing/hiding of your bottom nav based on what destination you're on? That's exact the use case we describe at https://github.com/google/accompanist/issues/633#issuecomment-942988181
f
I read that thread too. This feature is very nice, but does not cover all use cases imo, e.g. for animating the incoming screen with a higher z-index than the bottom nav bar.
l
Sorry @Ian Lake, I didn't mean to ask something that we already discussed, but I was only curious if we have another way to do this without animation
It seemed a little strange to me to have a Scaffold with BottonNav and need to hide or show depending on the current route
i
Isn't that exactly what you said you want to happen? "with a BottomNavigation only on certain screens" were your exact words
l
Yes, this solves that problem, but I was curious if we could use an Scaffold only on some screens without having to put the NavHost inside a Global Scaffold, instead put an Scaffold on part of the navigation/composable()
Sorry for creating other thread about something related to that 😥