https://kotlinlang.org logo
#compose
Title
# compose
l

Lucas Reis

10/28/2021, 2:10 PM
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

Felix Schütz

10/28/2021, 2:12 PM
Related to https://github.com/google/accompanist/issues/729. Unfortunately, the issue was auto-closed.
l

Lucas Reis

10/28/2021, 2:34 PM
Nice, I'll try to add some interest on the issue. How did you solve this problem with two NavHosts()?
f

Felix Schütz

10/28/2021, 2:43 PM
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

Lucas Reis

10/28/2021, 2:49 PM
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

Felix Schütz

10/28/2021, 2:54 PM
As far as I'm concerned, you can't share you NavGraph, so you need to NavControllers.
l

Lucas Reis

10/28/2021, 2:55 PM
sorry, I was not clear hehe I meant, can you share your Navigation code?
f

Felix Schütz

10/28/2021, 3:03 PM
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

Lucas Reis

10/28/2021, 3:05 PM
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

Felix Schütz

10/28/2021, 3:17 PM
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

Ian Lake

10/28/2021, 3:25 PM
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

Felix Schütz

10/28/2021, 3:28 PM
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

Lucas Reis

10/28/2021, 3:42 PM
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

Ian Lake

10/28/2021, 3:44 PM
Isn't that exactly what you said you want to happen? "with a BottomNavigation only on certain screens" were your exact words
l

Lucas Reis

10/28/2021, 3:47 PM
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 😥