Communicate from composable to fragment using viewmodel
I am having a Composable
@Composable
fun SimpleBottomButton(SCVM: StudioComposeViewModel) {
Button(onClick = {
SCVM.setbottomButtonClick(2)
}) {
Text(text = "Bottom Button")
}
}
and a viewmodel
class ComposeViewModel(
savedStateHandle: SavedStateHandle,
) : ViewModel() {
private val _bottomButtonClick = MutableStateFlow(
savedStateHandle["BottomButtonClick"] ?: 0
)
val bottomButtonClick
get() = _bottomButtonClick.asStateFlow()
fun setbottomButtonClick(valuer: Int) {
_bottomButtonClick.value = valuer
}
}
in the fragment I want to get the data from viewmodel in a Fragment using this
viewLifecycleOwner.lifecycleScope.launch {
SCVM.bottomButtonClick.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED)
.collect {
Log.e("bottomButtonClick",it.toString())
Toast.makeText(requireActivity(), it.toString(),Toast.LENGTH_SHORT).show()
}
}
However the data is not received