I have thought that is okay to collect SharedFlow data on onViewCreated. But when i replace fragment n times then fire some event to SharedFlow, it emits n times event to me instead of one event.
I have fixed it how i put my code on onCreate at my Fragment. But i haven't found any documentation about that. Am i missing something or i should keep using SharedFlow collections at onCreate in Fragment ?
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launchWhenResumed {
viewModel.uiEffect.collect {
when (it) {
is ViewEffectWrapper.PageEffect -> {
if (it.pageEvent is LoginViewEffect.OpenHomePage) {
startActivity(
Intent(
this@LoginFragment.context,
HomeActivity::class.java
)
)
}
}
}
}
}
}
And here is my SharedFlow definition at ViewModel
private val _uiEffect = MutableSharedFlow<ViewEffectWrapper<LoginViewEffect>>(replay = 0)
val uiEffect: SharedFlow<ViewEffectWrapper<LoginViewEffect>> = _uiEffect