dave08
02/26/2024, 11:35 AMval context = LocalContext.current
BackHandler(enabled = true) {
if (!navigator.popBackStack()) {
(context as MainActivity).finish()
}
}
in my HomeScreen composable, it takes a bunch of back presses to exit the app... how do I handle this in compose directions?Rafael Costa
02/26/2024, 11:45 AMStylianos Gakis
02/26/2024, 11:45 AMdave08
02/26/2024, 11:47 AMyou can follow the suggestions that androidx.navigation suggests in the first place?Which suggestions are you referring to?
logging the contents of the back stack.Yeah, I guess I'll have to do that, I was just wondering if this is maybe a known problem...
Stylianos Gakis
02/26/2024, 11:47 AMStylianos Gakis
02/26/2024, 11:49 AMRafael Costa
02/26/2024, 11:49 AM> logging the contents of the back stack.
Yeah, I guess I'll have to do that, I was just wondering if this is maybe a known problem...I usually always have this on debug builds at least. Really helps me understand whats going on. Sometimes there's something I wasn't expecting 🙂
Rafael Costa
02/26/2024, 11:50 AMdave08
02/26/2024, 11:57 AMRafael Costa
02/26/2024, 12:00 PM@SuppressLint("RestrictedApi")
@Composable
fun LogBackStack(navController: NavController) {
LaunchedEffect(navController) {
navController.currentBackStack.collect {
it.print()
}
}
}
fun Collection<NavBackStackEntry>.print(prefix: String = "stack") {
val stack = toMutableList()
.map {
val route = it.route()
val args = runCatching { route.argsFrom(it) }.getOrNull()?.takeIf { it != Unit }?.let { "(args={$it})" } ?: ""
"$route$args"
}
.toTypedArray().contentToString()
println("$prefix = $stack")
}
Rafael Costa
02/26/2024, 12:03 PMdave08
02/26/2024, 12:04 PMdave08
02/26/2024, 2:04 PMRafael Costa
02/26/2024, 2:04 PMdave08
02/26/2024, 2:06 PMdave08
02/26/2024, 2:06 PMRafael Costa
02/26/2024, 2:08 PMRafael Costa
02/26/2024, 2:09 PMRafael Costa
02/26/2024, 2:09 PMRafael Costa
02/26/2024, 2:11 PMval stack = toMutableList()
.map { it.route().route }
Rafael Costa
02/26/2024, 2:11 PMdave08
02/26/2024, 2:14 PMRafael Costa
02/26/2024, 2:16 PMRafael Costa
02/26/2024, 2:16 PMRafael Costa
02/26/2024, 2:16 PMdave08
02/26/2024, 2:20 PMRafael Costa
02/26/2024, 2:22 PMdave08
02/26/2024, 2:23 PMdave08
02/26/2024, 2:24 PMRafael Costa
02/26/2024, 2:25 PMdave08
02/26/2024, 2:26 PMRafael Costa
02/26/2024, 2:27 PMdave08
02/26/2024, 2:30 PMRafael Costa
02/26/2024, 2:31 PMRafael Costa
02/26/2024, 2:32 PMStylianos Gakis
02/26/2024, 2:37 PMdave08
02/26/2024, 2:43 PMIf it’s part of your graph, when you navigate to it, as you do it you gotta pop the entire backstack before you go there so that if you press back you end up outside of your app instead of back to where you came fromIt's not an authentication that a user does, but rather a type of loading screen that does authentication with credentials already provided. If creds are invalidated, then it goes to another activity anyways...
Stylianos Gakis
02/26/2024, 2:51 PMdave08
02/26/2024, 2:52 PM