Kshitij Patil
12/04/2020, 3:24 PMcollectAsState()
in it? Do I get a snapshot of snapshot of state and won't receive any further updates or will do?
I've a navigation graph definition using navigation-compose and am using different viewModel for each of its screens. I'm doing collectAsState
in the content parameter of NavGraphBuilder.comoposable()
extension function. I want the respective Composable in the navgraph to be notified on any state changes in its associated viewModel. I checked every step in the process of mutating the state and viewModel state is working properly but the Composable is not listening to these changes and thus not being updated.
How should this be achieved in general? Individual Composables listening to their own ViewModels when they're part of some navigation hierarchy like BottomNavigation ?Adam Powell
12/04/2020, 3:34 PMState<T>
and a LaunchedEffect
collects the flow and writes into the state object. Compose sees the state changes and recomposes. Can you post the code that is behaving unexpectedly?Kshitij Patil
12/04/2020, 4:12 PMJoost Klitsie
12/05/2020, 7:22 AMKshitij Patil
12/05/2020, 8:18 AMNavHost
and then I distributed the respective fields to composables in navgraph assuming that only those composables will be notified when respective field is changed. However, since the Composable itself was taking in the state, it turned out to be recomposed on any field changes.
Instead of passing in state, I kept that at parent Composable only and used "slots" to write individual pages and passed in respective state fields directly into those slots. This way, only associated pages got recomposed and everything just worked. But, since I moved large chunk of code from the NavHost to parent itself in the form of slots, it resulted in verbose code in the same file.