Do you guys just define a `TopAppBar` for each of ...
# compose
a
Do you guys just define a
TopAppBar
for each of your screen? Or share a single one and somehow manage to customize it (set title, navigation icon)? I’m trying to do the latter with
compose-navigation
in a multi-modular context, and now contemplating whether this is really a good idea…
k
I used to have a shared TopAppBar but it got complicated when I needed it to contain some buttons/search/etc for some of the screens. After that I switched to TopAppBar per screen and promised myself that I'll one day investigate if there is a better approach. But I never did. 😅
l
You can easily create one TopAppBar that can have custom actions per screen:
Copy code
fun MyTopAppBar(
    modifier: Modifier,
    backgroundColor: Color,
    elevation: Dp,
    onBackPressed: () -> Unit,
    actions: @Composable RowScope.() -> Unit = {},
) {
    TopAppBar(
        modifier = modifier,
        backgroundColor = backgroundColor,
        elevation = elevation,
        contentColor = MaterialTheme.colors.onSurface,
        navigationIcon = {
            IconButton(
                modifier = Modifier.background(
                    color = MaterialTheme.colors.surface,
                    shape = CircleShape
                ),
                onClick = { onBackPressed() },
            ) {
                Icon(
                    imageVector = Icons.Filled.ArrowBack,
                    contentDescription = null,
                    tint = MaterialTheme.colors.onSurface,
                )
            }
        },
        actions = actions,
    )
}
a
that's just a generic function that you still call for each screen. Currently the former means the app bar transitions with the screen (when it's available). I wonder whether I should jusy do the former, hoping shared transitions api could enable the TopAppBar of each screen to remain in place while the rest of the content slides.
b
I tend towards a unique app bar per screen. The contents of your app bar are generally speaking intrinsically tied to the screen they are on (title, actions, etc), so there isn’t a lot of value in sharing a single AppBar across all screens. With compose it is easy to share the same
Composable
function without sharing the same instance of the app bar
a
Same as others, same as I do on regular View way for fragments... appBar for each ono