Ky
08/12/2021, 10:46 PMsetContent{}
. I have a shared viewModel between a Fragment and an Activity. In the activity xml I have a ComposeView
and in onStart
I’m attempting to update that ComposeView like this
Code block 1
Inside the composable view is something like
Code block 2
The problem is that setContent
in the activity is only being called when the activity is attached/detached. It is not observing the data stream but if it switch the code to this(still in onStart):
Code block 3
This works fine. Is there something I’m missing about setContent{} in the Activity?kevindmoore
08/12/2021, 10:57 PMKy
08/12/2021, 10:57 PMKy
08/12/2021, 10:59 PMviewFromXml.setContent {
Theme {
ComposableView(
viewModel = viewModel
) { someVal ->
doSomeStuff(someVal)
}
}
}
ComposableView(
viewModel: ViewModel){
val state = viewModel.subscribeAsState
...
...
viewModel.subscribe({ state ->
viewFromXml.apply {
setContent {
Theme {
Composableview(
state = state
) { someVal ->
doSomeStuff(someVal)
}
}
}
}
}
kevindmoore
08/12/2021, 11:04 PMKy
08/12/2021, 11:15 PM<ComposeView>
at the activity level and the changes in this SharedViewModel state are coming from a Fragment. The Fragment which shares this ViewModel, observes the state changes just fine.
If I trigger the Activity lifecycle changes, onStop
and then back to onStart
. Then setContent{}
called again and the ComposableView(viewmodel)
then observes the latest state and updates.