https://kotlinlang.org logo
#compose
Title
# compose
e

E Smits

06/17/2020, 9:32 AM
Hi I'm new to Jetpack Compose, I wonder if there is anybody that found a way to handle navigation backstack with the hardware button? And now while we are at it, is the navigation lib going to be connected with Jetpack Compose later on? For now I am using the method from the sample:
Copy code
object NavStatus {
    var currentScreen by mutableStateOf<Screen>(Screen.Home)
}

fun navigateTo(destination: Screen) {
    NavStatus.currentScreen = destination
}
This is fine if you specify onclicks in the UI. However the hardware button closes the app. Usually you would handle an onKeyListener in your fragments or onBackPress in an activity. I could imagine that you could keep an array of recent navigated screens and just pop one using the onbackpress listener in the MainActivity.
t

Theo

06/17/2020, 9:37 AM
you could take a look into this and see how he's implemented it, https://github.com/zsoltk/compose-router
I assume it'l be part of compose at some stage
e

E Smits

06/17/2020, 9:43 AM
Alright thanks! Will look into it
i

Ian Lake

06/17/2020, 1:42 PM
Yes, integration with Navigation is planned
💯 4
v

Vinay Gaba

06/17/2020, 4:25 PM
For the onBackPressed case in particular, Adam had shared an example of what an implementation could look like today - https://kotlinlang.slack.com/archives/CJLTWPH7S/p1591558155394500?thread_ts=1591558024.394400&amp;cid=CJLTWPH7S
e

E Smits

06/17/2020, 5:05 PM
Thanks, what I ended up doing was (what has to be done in order to keep our company clean-mvvm template) keep using fragments but build them up using compose. This allows me to use the current androidx navigation lib alongside many other aspects like viewmodels, dependency injection etc that we are used to at the company while still grasping the benefits of an reactive UI from compose.
1
2 Views