Compose Part 4
Q31. How do you implement a bottom navigation bar with multiple back stacks using Compose Navigation?
@Composable
fun MultiStackBottomNav() {
val tabs = listOf("Home", "Search", "Profile")
var selectedTab by rememberSaveable { mutableIntStateOf(0) }
// One NavController per tab, remembered across recomposition.
val navControllers = remember {
List(tabs.size) { mutableStateOf<<Bundle?>(null) }
}.mapIndexed { index, savedState ->
rememberNavController().apply {
// Restore previous state if available
savedState.value?.let { restoreState(it) }
}
}
// Save state when switching away from a tab.
DisposableEffect(selectedTab) {
onDispose {
navControllers.forEachIndexed { i, ctrl ->
if (i != selectedTab) {
// In a real implementation, you'd save nav state bundles here.
}
}
}
}
Scaffold(
bottomBar = {
NavigationBar {
tabs.forEachIndexed { index, label ->
NavigationBarItem(
selected = selectedTab == index,
onClick = { selectedTab = index },
icon = { Icon(Icons.Default.Home, contentDescription = label) },
label = { Text(label) }
)
}
}
}
) { padding ->
Box(Modifier.padding(padding).fillMaxSize()) {
tabs.forEachIndexed { index, _ ->
if (index == selectedTab) {
TabNavHost(
navController = navControllers[index],
startDestination = "${tabs[index].lowercase()}_root"
)
}
}
}
// Intercept system back for the active tab.
BackHandler {
if (!navControllers[selectedTab].popBackStack()) {
// If back stack is empty, maybe exit or switch to first tab.
}
}
}
}
@Composable
fun TabNavHost(navController: NavHostController, startDestination: String) {
NavHost(navController = navController, startDestination = startDestination) {
composable("home_root") { HomeTab() }
composable("home_detail/{id}") { DetailScreen(it.arguments?.getString("id")!!) }
composable("search_root") { SearchTab() }
composable("profile_root") { ProfileTab() }
}
}Q32. How do you embed a View inside Compose and vice versa? What are the lifecycle implications?
View inside Compose and vice versa? What are the lifecycle implications?Q33. What is AndroidView and when should you prefer it over rewriting a custom view in Compose?
AndroidView and when should you prefer it over rewriting a custom view in Compose?Q34. How do you gradually migrate a large XML-based codebase to Compose without a full rewrite?
Q35. What are the challenges of using Compose inside RecyclerView or ViewPager2?
RecyclerView or ViewPager2?Q36. How do you handle WindowInsets when mixing Compose and legacy View-based screens?
WindowInsets when mixing Compose and legacy View-based screens?Q37. How do you write unit tests for Composables? What is the role of createComposeRule?
createComposeRule?Q38. What is semantic testing in Compose, and how do you use SemanticsMatcher and SemanticsNodeInteraction?
SemanticsMatcher and SemanticsNodeInteraction?Q39. How do you test state hoisting and verify that state changes trigger recompositions correctly?
Q40. What are the limitations of Compose UI testing compared to Espresso, and how do you work around them?
Last updated