I'm using navigation-compose and I basically have ...
# compose-android
c
I'm using navigation-compose and I basically have a really simple app. It has a home screen as the start destination, but it also has an onboarding wizard. In my home screen, I have a launchedEffect that will navigate you to onboarding destionation if you haven't yet gone through onboarding. That all works as expected BUT I'm not sure how to "close" the app when the user hits the system back button while on the onboarding screen. Should I just do
Copy code
val context = LocalContext.current
    BackHandler {
        (context as Activity).finish()
    }
in my onboarding?
Before anyone asks, yes I'd like to prevent the user from skipping the onboarding via the backbutton. We should indeed exit.
If I don't do anything, then everytime you press back, you see a flash and you're basically navigated back to the onboarding screen again.
b
Can you just change the start destination of the nav graph instead?
c
I'm pretty sure Ian + nav docs advise not to use conditional start destinations. Instead you should go to your "main" location and at that location check if you should be logged in, or w/e. I think that still holds true today
👍 1
for example

https://youtu.be/09qjn706ITA?si=12dc_K4TRKV0iIvl&t=289

b
You are right! I forgot about that. It's been awhile obviously. In that case, can't you set your nav action to be popupto inclusive so it pops off the first screen as well?
👍 1
s
Try this indeed ^^ That's what I do for a login scenario, just pop all the way to your start destination too. Then back will properly do a predictive back out of your app. When finishing your onboarding make sure to again pop the entirety of your onboarding, and go back to the start destination.
c
popUpTo and stuff always confuses me. lol. Maybe because ESL. but let me give that a go!
Currently I'm doing the activity finish() and that seems to work alright.
Actually... this kinda setup doesn't really work If my "main" screen has
Copy code
val hasOnboarded by prefManager.onboarded.collectAsState(initial = false)

LaunchedEffect(hasOnboarded) {
    if (!hasOnboarded) {
        navigateToOnboardingLambda()
    }
}
then since i start with inital = false, then every time i get to this screen it'll be false and go right back to onboarding screen. HMMM
s
Sounds like you want a third state, of it being undefined. But this doesn't have much to do with your original question of course. The finish does work, but you're breaking predictive back gesture animation when exiting the app.
c
Sounds like you want a third state, of it being undefined
Damn you @Stylianos Gakis for making so much sense. lol. It's been a longggg week. My brain is melted
🫠 1
😆 1