Reputation: 59
The setContent in the Observer is not being executed after being attached to the liveData. So I'm just getting a blank activity. What's going on here? It seems to work in another activity with a similar pattern.
class TeamBuilderActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val imageIds = listOf(
R.mipmap.goblin_smiling_background,
R.mipmap.marker_goblin_axe_foreground,
R.mipmap.beholder_foreground,
R.mipmap.d20_background,
R.mipmap.marker_goblin_axe_foreground,
R.mipmap.goblin_smiling_foreground
)
val monsters = listOf(
GoblinoidFactory.Bugbear(), GoblinoidFactory.Goblin(), KoboldFactory.Kobold()
)
val viewModel = ViewModelProvider(this).get(TeamBuilderActivityViewModel::class.java)
val liveData = viewModel.getLiveData()
liveData.observe(this, Observer {
setContent {
Column {
Card {
Column(Modifier.padding(5.dp)) {
TeamBuilderComposables.ImageSlider(
imageIds = imageIds,
onClicked = viewModel::onClickImage
)
TeamBuilderComposables.RandomNameRoller(
inputStream = resources.openRawResource(R.raw.nicknames),
onClicked = viewModel::setName
)
TeamBuilderComposables.StatBlockChooser(
monsters = monsters,
onClicked = viewModel::onClickMonsterStatBlock
)
}
}
TeamBuilderComposables.CharacterInfo(
imageId = it.selectedImageId,
monster = it.selectedMonster,
name = it.selectedName
)
}
}
})
}
}
Upvotes: 1
Views: 557
Reputation: 59
OK … I found that the best way to handle this is to turn the live data into a state object inside the compose block with:
val viewState = liveData.observeAsState()
Upvotes: 1