From 189de89466b336c90dd5eb079eb3c23305c59883 Mon Sep 17 00:00:00 2001 From: kari-ts <135075563+kari-ts@users.noreply.github.com> Date: Thu, 6 Mar 2025 11:33:56 -0800 Subject: [PATCH] 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 --- .../java/com/tailscale/ipn/MainActivity.kt | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/tailscale/ipn/MainActivity.kt b/android/src/main/java/com/tailscale/ipn/MainActivity.kt index bea2925..7a68160 100644 --- a/android/src/main/java/com/tailscale/ipn/MainActivity.kt +++ b/android/src/main/java/com/tailscale/ipn/MainActivity.kt @@ -365,11 +365,23 @@ class MainActivity : ComponentActivity() { override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) if (intent.getBooleanExtra(START_AT_ROOT, false)) { - if (this::navController.isInitialized) { - navController.popBackStack(route = "main", inclusive = false) - } + if (this::navController.isInitialized) { + val previousEntry = navController.previousBackStackEntry + TSLog.d("MainActivity", "onNewIntent: previousBackStackEntry = $previousEntry") + + if (previousEntry != null) { + navController.popBackStack(route = "main", inclusive = false) + } else { + TSLog.e("MainActivity", "onNewIntent: No previous back stack entry, navigating directly to 'main'") + navController.navigate("main") { + popUpTo("main") { inclusive = true } + } + } + } } - } +} + + private fun login(urlString: String) { // Launch coroutine to listen for state changes. When the user completes login, relaunch