I am trying to implement a very simple Master/Deta...
# compose
d
I am trying to implement a very simple Master/Detail app in Compose, where the Master screen is showing a list of items and the Detail screen is showing the details of the selected item. I am using a MultiPlatform ViewModel, where I would like to keep the state of which is the current screen that is shown. I am considering using just 1 Activity for the whole app. In order to manage the navigation I also need to listen to the BackButton. Can you please suggest what is the recommend way in Compose to get the BackButton event? Would it be the
onBackPressed(){...}
method of the Activity without calling
super.onBackPressed()
?
👍 1
s
+1 to the question about adaptive ui. Are there any Compose related best practices how to build UI which may adapt to smartphone screen, tablet or a chromebook device?
...or even to wearable device screen.
f
JetChat has an implementation using
OnBackPressedCallback
d
It seems quite a cumbersome approach. it seems much handier to just pass the event back to the ViewModel
Copy code
override fun onBackPressed() {
    appViewModel.coreModel.pressedBackButton()
}
f
There are of course different ways how one could implement this. However you should consider the possibility that users also might want to leave the App if they press back on the topmost screen (since you are never calling
super.OnBackPressed
)
d
you are right, this is a very simplified version
f
The upside of the JetChat implementation is that you can use the
backPressHandler
basically anywhere in your
Compose
Tree (without having to pass it down the tree), since it uses an
Ambient
d