Hi guys i have some issues with navigation and may...
# compose
a
Hi guys i have some issues with navigation and maybe im doing it all wrong. I have a stack app that have a bottomBar on it. All works as expected i can push from login to next screen and show the 3 screens BottomNavigationBar. My issue is that every time i change tabs. my components are recomposed eventho i have set this on the navBottomNavigationItem
launchSingleTop = true
. if i only run the 3 screens NavigationBar screen without any page before on the stack they are NOT recomposed every time i change the tab. have any of you seen this issue before?
a
What’s your issue with recomposition? That can happen anytime.
a
i would like to keep the state when i change tabs, e.g in the tab num 2 i have a list of itms if i scroll to the bottom and i switch to other tab and come back to the 2nd one. i would like the app to remember that the list was scrolled to that point instead of reseting everything to initial state
and that actually happens if i only have the bottomNavigation. i can change tabs and it remembers the state. but if i have an stack of screens. the bottomNavigation state wont work 😕
a
I see. Retaining state is different from recomposition. If you would like to retain state, you could use a `ViewModel`or
rememberSaveable
which is scoped to the parent graph
If a recomposition resets your state, then a simple config change (like rotating device) will reset everything in your screen.
a
i see ill have a look to the
rememberSaveable
,just got confused why it works when only bottomNavigation is on top of the graph but when something else is on top it resets the state 🤷‍♀️
a
Ah, btw, didn’t notice you’re doing something with the Login. The recommendations for designing navigations requiring authentication are different than your graph. You can read the guidelines here. https://developer.android.com/guide/navigation/navigation-conditional
Are you using compose-navigation to navigate between bottom bar screens?
a
😻 i didnt know about that best practice 🙂
👍 1
yes, i am
a
so in short for logging screen you show it conditionally for every screen that requires auth, instead of being part of the static navigation (simplifies things like deeplinks)
And as to why it’s recomposing (or actually, losing the state). If the destination/NavBackStackEntry is removed from the stack, then the state isn’t retained. And the bottom bar behavior is that there is only one screen at a time and it doesn’t make the stack of previous bottom bar navigations.
Also, there is an upcoming support for multiple backstacks, which you could also use for your bottom bar navigation
a
Thanks Allan you are a lifesaver, i was using a lot of time to fix this and i can clearly see that i was all wrong 🙂 thanks for the links and references
🙏 1
👍 1
a
For the googlers, feel free to correct me if I’m wrong somewhere here 😅
i
Here's the last discussion (including a few more links with more background) around login: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1617812338353700?thread_ts=1617805109.343700&cid=CJLTWPH7S
But yes, when it comes to saving the state of each bottom nav item as you swap between them, you'll want to wait for this next release and the new APIs to save/restore state that are part of that multiple back stacks bug
c
@Alexa_Gal you only have to wait until the next release for multiple back stacks. Some of us have been waiting for 👀... way longer. 😂
😅 1
a
Just in case, you may try the Decompose library, it allows to handle this case correctly.
👀 1