From 1db3c0be3e5fc3bd9f4f4fbb23ddc08e0ca87358 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 23 Oct 2026 23:11:57 +0000 Subject: [PATCH] app: switch tabs on new intents MainActivity is singleTask, so an intent sent while the app is running is delivered to onNewIntent(). The requested tab was only read from the launch intent, so e.g. the "App settings" button in the system settings brought the app to the front without opening the Settings tab. Also read the requested tab from new intents and switch to it. If another screen is open, it stays open and the tab is shown when going back, so no running flash or action screen is interrupted. Assisted-by: Claude Opus 5.5 --- .../java/com/topjohnwu/magisk/ui/MainActivity.kt | 13 ++++++++++--- .../main/java/com/topjohnwu/magisk/ui/MainScreen.kt | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt index f0bbd8fdb..849515df5 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -77,6 +77,8 @@ class MainActivity : ComponentActivity(), SplashScreenHost { override val splashController = SplashController(this) private val intentState = MutableStateFlow(0) + // A tab requested by a new intent while the activity is running (it's singleTask) + private val tabRequest = MutableStateFlow(null) internal val showUnsupported = MutableStateFlow>>(emptyList()) override fun attachBaseContext(base: Context) { @@ -116,7 +118,7 @@ class MainActivity : ComponentActivity(), SplashScreenHost { } } - val initialTab = getInitialTab(intent) + val initialTab = getRequestedTab(intent) ?: Tab.HOME.ordinal setContent { MagiskTheme { @@ -150,8 +152,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { val superuserVm: SuperuserViewModel = viewModel( viewModelStoreOwner = this@MainActivity, factory = VMFactory ) + val requestedTab by tabRequest.collectAsStateWithLifecycle() MainScreen( initialTab = initialTab, + requestedTab = requestedTab, + onTabRequestHandled = { tabRequest.value = null }, superuserViewModel = superuserVm, ) } @@ -336,10 +341,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) setIntent(intent) + getRequestedTab(intent)?.let { tabRequest.value = it } intentState.value += 1 } - private fun getInitialTab(intent: Intent?): Int { + private fun getRequestedTab(intent: Intent?): Int? { val section = when { intent?.action == Intent.ACTION_APPLICATION_PREFERENCES -> Const.Nav.SETTINGS intent?.action == FlashUtils.INTENT_FLASH && @@ -347,10 +353,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { else -> intent?.getStringExtra(Const.Key.OPEN_SECTION) } return when (section) { + Const.Nav.HOME -> Tab.HOME.ordinal Const.Nav.SUPERUSER -> Tab.SUPERUSER.ordinal Const.Nav.MODULES -> Tab.MODULES.ordinal Const.Nav.SETTINGS -> Tab.SETTINGS.ordinal - else -> Tab.HOME.ordinal + else -> null } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt index 197b0e2a2..30f80aa44 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt @@ -45,6 +45,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusDirection @@ -91,6 +92,8 @@ enum class Tab(val titleRes: Int, val iconRes: Int) { fun MainScreen( modifier: Modifier = Modifier, initialTab: Int = Tab.HOME.ordinal, + requestedTab: Int? = null, + onTabRequestHandled: () -> Unit = {}, superuserViewModel: SuperuserViewModel? = null, ) { val navigator = LocalNavigator.current @@ -106,6 +109,15 @@ fun MainScreen( } val initialPage = visibleTabs.indexOf(Tab.entries[initialTab]).coerceAtLeast(0) val pagerState = rememberPagerState(initialPage = initialPage, pageCount = { visibleTabs.size }) + + // Show a tab requested while the app is already running, e.g. by the system settings + val currentOnTabRequestHandled by rememberUpdatedState(onTabRequestHandled) + LaunchedEffect(requestedTab) { + if (requestedTab == null) return@LaunchedEffect + val page = visibleTabs.indexOf(Tab.entries[requestedTab]) + if (page >= 0) pagerState.scrollToPage(page) + currentOnTabRequestHandled() + } val fabFocusRequester = remember { FocusRequester() } val navModulesFocusRequester = remember { FocusRequester() } val navSuperuserFocusRequester = remember { FocusRequester() }