Is there anything in compose like SwiftUI observab...
# compose
j
Is there anything in compose like SwiftUI observable objects. The observable objects can be used as singletons and there values, when changed can update UI states across the app. Other wise it looks like I have to place callback all over my app. Is there any way over snaking callbacks all over then place?
n
I think you’re looking for
staticCompositionLocalOf
or `compositionLocalOf`… https://medium.com/mobile-app-development-publication/android-jetpack-compose-compositionlocal-made-easy-8632b201bfcd
👍 1
j
Looks promising. I'll read up on it
s
How about using Android View Model architecture component and declare it in a top level composable (eg. the composable holding your NavHost) or in Main Activity and then passing the View Model as an argument to all the composable that need the shared state across views/composables.
n
What I’m currently doing is: • Do my best to have one view model per screen • If the previous is not possible, using navigation + hilt you can scope your view model based on your back stack navigation entry. • If you need a wider scope for the view model, create a subgraph and scope the vm in this subgraph