Billy Newman
11/03/2023, 1:53 PM@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBar(
title: String,
navigationIcon: ImageVector? = null,
onNavigationClicked: (() -> Unit)? = null,
) {
TopAppBar(
title = {
Text(
text = title,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.titleMedium
)
},
navigationIcon = {
navigationIcon?.let { icon ->
IconButton(onClick = { onNavigationClicked?.invoke() } ) {
Icon(icon, contentDescription = "Navigation")
}
}
}
)
}
Compose screen with only TopBar
@Composable
fun Screen(
onBack: () -> Unit,
) {
Log.i("Compose", "screen compose")
Column {
TopBar(
title = "Screen",
navigationIcon = Icons.Default.ArrowBack,
onNavigationClicked = { onBack() }
)
}
}
With this I see the compose happen 3 times. However if I remove the option onNavigationClicked parameter its only composed once.ascii
11/03/2023, 2:31 PMonBack
may not be memoized. Where does that function come from?
Try wrapping it in rememberUpdatedState
or simply remember(keys used in block) { block }
Billy Newman
11/03/2023, 2:33 PMcomposable("myRoute?id={id}") { backstackEntry ->
backstackEntry.arguments?.getString("id")?.let { id ->
Screen(
onBack = { navController.popBackStack() },
)
}
}
ascii
11/03/2023, 2:37 PMstable
, so that could be why the lambda isn't memoizedBilly Newman
11/03/2023, 2:40 PMascii
11/03/2023, 2:42 PM@Composable
inline fun <T> rememberCallback(
key1: Any?,
noinline callback: @DisallowComposableCalls () -> T,
) = remember(key1) { callback }
Billy Newman
11/03/2023, 3:18 PMascii
11/03/2023, 4:24 PMTestNavHostController
. We don't have any integration tests for nav stuff yet, so it works well for us.Billy Newman
11/03/2023, 6:06 PM