Skip to content

Commit 189de89

Browse files
authored
android: check back stack before popping back (#618)
A possible cause for an IndexOutOfBounds exception is that when the intent is delivered, the back stack doesn't have enough entries for the operation. Condition popping back on if there is more than one entry in the back stack; otherwise, navigate directly to main to reset the back stack. Updates tailscale/tailscale#15188 Signed-off-by: kari-ts <[email protected]>
1 parent 10b2c61 commit 189de89

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

android/src/main/java/com/tailscale/ipn/MainActivity.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,23 @@ class MainActivity : ComponentActivity() {
365365
override fun onNewIntent(intent: Intent) {
366366
super.onNewIntent(intent)
367367
if (intent.getBooleanExtra(START_AT_ROOT, false)) {
368-
if (this::navController.isInitialized) {
369-
navController.popBackStack(route = "main", inclusive = false)
370-
}
368+
if (this::navController.isInitialized) {
369+
val previousEntry = navController.previousBackStackEntry
370+
TSLog.d("MainActivity", "onNewIntent: previousBackStackEntry = $previousEntry")
371+
372+
if (previousEntry != null) {
373+
navController.popBackStack(route = "main", inclusive = false)
374+
} else {
375+
TSLog.e("MainActivity", "onNewIntent: No previous back stack entry, navigating directly to 'main'")
376+
navController.navigate("main") {
377+
popUpTo("main") { inclusive = true }
378+
}
379+
}
380+
}
371381
}
372-
}
382+
}
383+
384+
373385

374386
private fun login(urlString: String) {
375387
// Launch coroutine to listen for state changes. When the user completes login, relaunch

0 commit comments

Comments
 (0)