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
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 1d662cb09d
commit 1db3c0be3e
2 changed files with 22 additions and 3 deletions
@@ -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<Int?>(null)
internal val showUnsupported = MutableStateFlow<List<Pair<Int, Int>>>(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
}
}
@@ -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() }