'SharedFlow in Android project not working as expected

I was trying to pass events from UI to viewModel using sharedFlow this is my viewmodel class

class MainActivityViewModel () : ViewModel() {
    val actions = MutableSharedFlow<Action>()
    private val _state = MutableStateFlow<State>(State.Idle)
    val state: StateFlow<State> = _state

    init {
        viewModelScope.launch { handleIntents() }
    }

    suspend fun handleIntents() {
        actions.collect {
            when (it) {...}
       }
   }
}

and this is how i am emiting actions

private fun emitActions(action: Action) {
        lifecycleScope.launch {
        vm.actions.emit(action)
        }
    }

For the first time emission happening as expected, but then it is not emitting/collecting from the viewmodel.

Am i doing anything wrong here??



Solution 1:[1]

When I used collectLatest() instead of collect() it worked as expected

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Boken