Tim Malseed
10/03/2022, 12:02 AMStylianos Gakis
10/03/2022, 8:08 AMNotLoggedIn
case, we can then press the back button on the āloginā screen right? What will this do? As I see it it will pop the back stack, then go to āhomeā, and home will quickly realize that weāre still not logged it and go to the āloginā screen again, making the back gesture basically do a quick flicker of going back and forth or even if this is too quick to flicker it will at least do nothing.
Am I missing something here?Tim Malseed
10/03/2022, 9:28 AMBackHandler
on the login screen, like so:
@Composable
fun LoginScreen (onExitApp: () -> Unit) {
BackHandler(enabled = true) {
onExitApp()
}
}
Where onExitApp()
propagates the event up the call hierarchy, back to the host Activity, which then calls finish()
Stylianos Gakis
10/03/2022, 9:51 AMTim Malseed
10/03/2022, 10:46 AM⢠On platform versions prior to, it finishes the current activity, but you can override this to do whatever you want.Build.VERSION_CODES.S
⢠Starting with platform versionSo, we could follow the same pattern, and call, for activities that are the root activity of the task and also declare anBuild.VERSION_CODES.S
withIntentFilter
andIntent#ACTION_MAIN
in the manifest, the current activity and its task will be moved to the back of the activity stack instead of being finished. Other activities will simply be finished.Intent#CATEGORY_LAUNCHER
moveTaskToBack()
, instead of `finish()`:
https://developer.android.com/reference/android/app/Activity#moveTaskToBack(boolean)Stylianos Gakis
10/03/2022, 10:57 AMTim Malseed
10/03/2022, 11:03 AMfinish()
below Android S, and moveTaskToBack()
above.
If youāre not on the launcher activity, I guess you can pass the nonRoot = true
flag into moveTaskToBack(nonRoot: ..)
?Tim Malseed
10/03/2022, 11:04 AMboolean: If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.nonRoot
Stylianos Gakis
10/03/2022, 11:05 AMTim Malseed
10/03/2022, 11:05 AMTim Malseed
10/03/2022, 11:06 AMStylianos Gakis
10/03/2022, 11:06 AMTim Malseed
10/03/2022, 11:08 AMnavController
, and going back to the start destination is ViewModel
to indicate that the user has failed to log in, and then render some different content on the āhomeā screen to reflect this.Tim Malseed
10/03/2022, 11:13 AMsavedStateHandle
of the AppViewModel
called, say, loginSuccessful
. Initially itās null (neither success nor failure). Upon navigating back from the login screen, you set loginSuccessful = false
.
Then, this becomes an additional ViewState
, say LoginFailed
. If this is your ViewState
, then you might render some other content to the user - like a login button and an explanation that the user needs to log inTim Malseed
10/03/2022, 11:14 AMStylianos Gakis
10/03/2022, 11:15 AMTim Malseed
10/03/2022, 11:17 AMloginSuccessful
flag is something Ian alludes to in the ānavigating navigationā video actually. I reckon doing this, and rendering some content to the user explaining that they need to login - and then relying on the system to handle exiting the app - this seems like the right way to do it.Tim Malseed
10/03/2022, 11:21 AMStylianos Gakis
10/03/2022, 11:33 AM