I've an Activity A, with it's ViewModel with StateFlow UI State implementation as said in Android's documentation.
class A_ViewModel: ViewModel() {
private val _uiState = MutableStateFlow(UIState.None)
val uiState: StateFlow<UIState> = _uiState
fun onButtonClicked() {
_uiState.value = UIState.NavigateToB
}
}
class A_Activity: AppCompatActivity() {
private val viewModel = // getViewModel()
override fun onCreate() {
button.setOnClickListener {
viewModel.onButtonClicked()
}
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { uiState ->
when (uiState) {
is UIState.NavigateToB -> {
startActivity(Intent(this, B::class.java))
}
.
.
else -> { /* do nothing */ }
}
}
}
}
}
}
The problem I'm facing is, when I return back from Activity B to Activity A, since viewModel.uiState.collect { }
is called again in onStart()
of Activity A, the latest value (UIState.NavigateToB
) gets replayed again and Activity B is again launched.
This seems to be a very trivial issue. But I'm unable to think of a safe solution for this.
Sure, there are successful work arounds like setting uiState.value = UIState.None
in the onStop()
of Activity A. But I'm unable to think of an elegant way of handling this.