Compose Part 2
Q11. Describe the purpose and limitations of CompositionLocal — when is it appropriate to use versus avoid?
CompositionLocal — when is it appropriate to use versus avoid?// Appropriate: Providing theme-related values or scroll state that many descendants need.
val LocalCustomColors = staticCompositionLocalOf<<CustomColors> { error("No colors provided") }
@Composable
fun AppTheme(content: @Composable () -> Unit) {
val colors = remember { CustomColors(primary = Color.Blue) }
CompositionLocalProvider(LocalCustomColors provides colors) {
MaterialTheme(content = content)
}
}
@Composable
fun ThemedButton(text: String, onClick: () -> Unit) {
val colors = LocalCustomColors.current // Clean, expected usage for theme tokens.
Button(onClick = onClick, colors = ButtonDefaults.buttonColors(containerColor = colors.primary)) {
Text(text)
}
}
// Avoid: Using CompositionLocal for business logic or ViewModels.
// val LocalUserViewModel = compositionLocalOf<UserViewModel> { error("Missing") }
// BAD: Hidden dependency makes testing and previewing impossible.Q12. How would you share state between sibling Composables without passing callbacks through multiple layers?
Q13. What is the difference between produceState, collectAsState, and collectAsStateWithLifecycle?
produceState, collectAsState, and collectAsStateWithLifecycle?Q14. How do you gracefully handle process death and restoration with Compose Navigation?
Q15. What is the "donut-hole skipping" optimization in Compose, and how do you ensure your code benefits from it?
Q16. How does LazyColumn differ from RecyclerView in terms of item recycling and composition strategy?
LazyColumn differ from RecyclerView in terms of item recycling and composition strategy?Q17. What is the cost of using Modifier chains extensively, and how do you optimize them?
Modifier chains extensively, and how do you optimize them?Q18. When should you use key in LazyColumn or LazyRow, and what happens if you omit it?
key in LazyColumn or LazyRow, and what happens if you omit it?Q19. How do you debug and fix recomposition loops or excessive recompositions in a large screen?
Q20. Explain the impact of using BoxWithConstraints or SubcomposeLayout on performance.
BoxWithConstraints or SubcomposeLayout on performance.Last updated