A bit of an architecture question I suppose. I hav...
# compose
c
A bit of an architecture question I suppose. I have this bottom sheet that's a picker and it can essentially be called from any screen.
Copy code
bottomSheet(route = Screen.NumberPickerBottomSheet.route) {
  NumberPickerBottomSheet()
}
In the bottom sheet when the "Save" button is hit, I want to update the number value to the screen/VM that started the bottomSheet. Since it was a navigation event that started the bottom sheet, I can't pass a lambda of like
onNumUpdateEvent
. What's the best way to go about this? The only other thing that comes to mind is for the screen/VM to implement some interface or something? Or maybe I just save the state in some Activity level VM? Edit: Or another option is to use the navController in the bottomsheet and use that to get the previous entry and get the VM/cast to the VM and update it that way?
1
👍 1
d
using compose navigation, you can use the savedStateHandle of the previousBackStackEntry to communicate to the screen that launches this picker destination
Copy code
NumberPickerBottomSheet(
    onPickNumber = { value ->
        navController.previousBackStackEntry?.savedStateHandle?.set(key, value)
    }
)
👍 1
in your previous destination (the one launching the picker), you can then observe the value from
navBackStackEntry.savedStateHandle
using the same key the picker used
c
Oh hm. Interesting. Do those values only get "transfered" once the bottom sheet collapses/goes away though? example: In the activity/fragment world with extras you only got the "extra" once the activity/fragment was closed.
a
One step backwards, but does this need to be in the navigation graph? Let's say this wasn't a bottom sheet and it was just used everywhere, it could be a normal composable with a lambda call back (i.e nothing to do with navigation). I haven't used bottom sheets in compose yet since I keep hearing they're nightmares so plz excuse this if the suggestion doesn't make sense
I ask since
navController.previousBackStackEntry
seems a lot like starting an activity/fragment for result, which I've never been a fan of from a state mgmt prospective
c
It could arguably just be in every destination yes. But I have a lot of screens (40) and so adding this to every screen seems subpar, although it would 100% be easier on the state management side of things.
👍 1
d
Oh hm. Interesting. Do those values only get "transfered" once the bottom sheet collapses/goes away though? example: In the activity/fragment world with extras you only got the "extra" once the activity/fragment was closed.
You can see the values immediately, because the previous destination is still active.
c
Thanks. appreciate the tips. TIL
m
I did something like this: https://gist.github.com/micHar/716d810f319973beb86ebbb1a5257be8 Although I'm exploring using a navgraph scoped viewmodel for communication between screens in a single navgraph and wondering what the most elegant approach is.