<@UJBPFB3SN> is there a built in Compose equivalen...
# compose
d
@Ian Lake is there a built in Compose equivalent for your answer about reselecting a bottom nav tab here https://stackoverflow.com/a/68779398 ? Or is there any guidance on how this should be implemented in Compose ?
d
There is no need for such thing in Compose. The Navigation bar in Compose is completely stateless unlike the xml. The individual NavigationBarItems have a selected and onClick parameters and it's completely up to you to decide what item should be selected and what should happen if the user clicks on them. Do you have a Compose code that you are trying to apply this on?
d
I'm just trying to understand if there's a "proper" way to achieve this really - the original feature request explicitly called this out https://issuetracker.google.com/issues/80029773
- pressing the tab again should reset its' backstack and scroll
But I can't find any guidance about this anywhere - nothing on the official docs talks about this at all and nothing on the official samples either.
So all in all, I was wondering if anyone has figured out the exact combination of values for
popUpTo
,
saveState
,
launchSingleTop
&
restoreState
that would achieve this resetting of the tab's backstack and scroll position (and properly cleans up any "savedState"s so that we don't end up with tons of them in memory).
i
You'll have to be a little more clear on what you are doing. If you are using Navigation Compose (e.g.,
NavHost
), then each tab is associated with a top level
@Serializable
class or a String, depending on whether you are using type safe navigation, but the concepts in that stack overflow question are exactly the same from Fragments to Compose since it is the exact same
NavController
they both use. So if you want to pop everything up to the top level class,
popBackStack
, just like that answer says, is enough
d
Right so it will be something among the lines of: • If the user is clicking on a different bottom nav tab (Tab B) than the currently selected (Tab A), navigate like the Compose docs used to say, e.g.
saveState
,
restoreState
and
launchSingleTop
all true +
popUpTo
starting destination • If the user is clicking on an already selected bottom nav tab (Tab A), just
popBackStack
to the top level destination, without worrying about
state
and there's nothing to "clean up"? So if after this the user clicks on Tab B again and then on Tab A, we won't see the "old" saved state of it again and instead it will expectedly show it's top level destination. That final bit of the 2nd bullet point is my real worry, don't understand enough of the saving and restoring mechanism to know if there's gonna by anything else that I'd need to manually do.
i
popBackStack
is what clears state
thank you color 1