It's okay to use launched effects to navigate when...
# compose
It's okay to use launched effects to navigate when a state changes? For example, a login view:
Copy code
val isLoginError by loginViewModel.loginError.observeAsState()
    val context = LocalContext.current
        when (isLoginError) {
            true -> {
                Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show()
            false -> navHostController.navigate(Paths.HOME_PATH)
            null -> { // blah }
That seems Ok. I’ve handled this sort of thing in my app by injecting a shared Navigation flow into my VMs. I can then emit Navigation directions to that flow directly from the VM which is collected at a global level and then the global collector calls navHostController as appropriate.
🙌 1
Can you share your solution? that looks like an interesting approach
To be fair, I didn’t come up with it. I think I found the idea in this article:
He shows a good example and explains why about halfway down the article.
So we can thank @Syex for this one. 🙏
it’s worked out great for testability and very easy to use within our application.
You're welcome 🙂
Thanks! I'll look into that 😄