How to collect two state flow in activity? Because mine only the first flow that consumed.
For example, inside viewmodel is like this:
class ExampleViewModel: ViewModel(){
private val state = MutableStateFlow<HomeMainFragmentState>(HomeMainFragmentState.Init)
private val products = MutableStateFlow<List<ProductEntity>>(mutableListOf())
//to be consumed
fun getState() : StateFlow<HomeMainFragmentState> = state
fun getProducts() : StateFlow<List<ProductEntity>> = products
}
And then in my view is like this:
private fun observe(){
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
viewModel.getState().collect { state -> handleState(state) }
viewModel.getProducts().collect{ products -> handleProducts(products) }
}
}
}
The problem is, only the first flow is consumed, for this case is the 'state', the products was never consumed/executed by activity/fragment.
How to fix this? I also read about combining the flow, does it mean the second flow depends on first flow to run?