One question. I have screen with few view that are...
# compose
j
One question. I have screen with few view that are rendering conditionally. Question now is that if go to View B from View A when press hardware back button want to go to View A . But if I am on View A and press back button want just to exit app. Can I apply this logic with BackHandler?
is it smart to do something like this
Copy code
BackHandler() {
    if(loginState.currentView === LoginView.EMAIL_VIEW.name) {
        loginViewModel.updateView(LoginView.LANDING.name)
    }
    else {
        activity?.finish()
    }
}
a
You can achieve that via onBackPressedDispatcher If you're using fragment/activity , but I don't know if these Apis exists in Compose or not.
i
The whole point of BackHandler (which is the Compose specific way to get to the OnBackPressedDispatcher) is to only enable it when you actually want to handle back, so your code should look like:
Copy code
BackHandler(enabled = loginState.currentView === LoginView.EMAIL_VIEW.name) {
  loginViewModel.updateView(LoginView.LANDING.name)
}
j
Thx a lot mate ❤️
i
The other option, of course, is to put the BackHandler directly within View B - once View B is removed from composition, its BackHandler gets disposed as well.