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

Colton Idle

01/19/2022, 3:32 PM
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

Desmond Teo

01/19/2022, 3:58 PM
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

Colton Idle

01/19/2022, 4:51 PM
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

Arjun Achatz

01/19/2022, 7:13 PM
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

Colton Idle

01/19/2022, 8:48 PM
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

Desmond Teo

01/20/2022, 2:31 AM
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

Colton Idle

01/20/2022, 3:49 AM
Thanks. appreciate the tips. TIL
m

Michal Klimczak

01/21/2022, 1:34 PM
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.
6 Views