adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.58k stars 136 forks source link

Question: TabNavigation + basic Navigation #82

Open raudonikis opened 2 years ago

raudonikis commented 2 years ago

I can't figure out how to have a top level Navigator that can navigate between basic screens, and a screen with a TabNavigator inside. Example use case, authentication flow:

What I've tried:

MainActivity:

setContent {
   AppTheme { Navigator(MainScreen) }
}

MainScreen:

val navigator = LocalNavigator.current
val screen = rememberScreen(...)

TabNavigator(tab = InitialTab) {
        Scaffold(
            content = { CurrentScreen() },
            bottomBar = { BottomBar() },
            FloatingActionButton(
                onClick = { navigator?.push(screen) }
            )
        )
    }

Navigation happens successfully, but for some reason it loses the Theme (dark theme becomes light theme), and also the state of the TabNavigator's last tab (if I navigate back, the Tab resets to the very first destination). Is there a better approach for handling this use case?

lcsphantom commented 2 years ago

In the documentation it says you are supposed to use CurrentTab instead of CurrentScreen when using TabNavigator.

Not sure if related to your issue but worth a shot.

raudonikis commented 2 years ago

In the documentation it says you are supposed to use CurrentTab instead of CurrentScreen when using TabNavigator.

Not sure if related to your issue but worth a shot.

Same issues with CurrentScreen and CurrentTab.

huyto-rh commented 2 years ago

I too am wondering how to do this. There doesn't seem to be a way to retrieve the parent Navigator when inside the TabNavigator's content since LocalNavigator.current will just return the TabNavigator's Navigator. The only way I've seen is to pass the parent Screen's Navigator to the children Tabs.

zakrodionov commented 1 year ago

@raudonikis check my test project https://github.com/zakrodionov/PracticalApp/tree/feature/compose_voyager_navigation I just did something similar recently.

https://user-images.githubusercontent.com/27068529/207821065-21fcba35-9c23-4c33-9a3b-c272168fc8a2.mp4

mkowol-n commented 11 months ago

Any update on this? I'm struggling with the issue when you pop the main navigator and you get the initial tab screen. I've created mini project: https://github.com/mkowol-n/voyagernav and i've noticed something.

Navigate to 2nd tab, navigate from red screen to white screen, go navigate to 1st tab. Now: Navigate from blackScreen to GreenScreen and then to YellowScreen. Press back button (you see black screen instead of greenscreen. However when you switch to 2nd tab - it didnt reset. It stays on white screen. So there has to be a bug somewhere in the voyager code, but i'm not sure where.

Btw: Same happens switch 1st tab with 2nd tab in the description above.

java30433 commented 9 months ago

It happens even without using TabNavigator, just nested Navigators. This problem doesn't seem to be related to tabs or anything like that, simply because there are nested Navigators. I hope anyone can solve it soon.