I have an app that is using Top App Bar and bottom navigation. Each bottom tab has its own navigation with NavHostController. On my Home tab, I have a NavHostController like so
fun HomeNavHostScreen(
modifier: Modifier = Modifier,
viewModel: HomeViewModel = hiltViewModel()
) {
val title = stringResource(id = R.string.home)
val navHostController: NavHostController = rememberNavController()
Scaffold(
topBar = {
B2CTopAppBar(
title = title,
navHostController = navHostController
)
},
content = {
NavHost(
navHostController,
startDestination = HomeRoute.HomeNavHostScreen.route,
modifier = Modifier.padding(top = it.calculateTopPadding())
) {
composable(HomeRoute.HomeNavHostScreen.route) {
HomeScreen()
}
composable(HomeRoute.DetailScreen.route) {
DetailScreen()
}
composable(HomeRoute.SomeOtherScreen.route) {
SomeScreen()
}
}
}
)
}
I set the title of Home Screen here, but for my detail screen I want to change the title to something else and add buttons to the TopAppBar for each child screen (DetailScreen, etc)
I tried to remove Scaffold from here and add it to each of my views separately To HomeScreen, Detail etc. But after i found that its not correct to have more than 1 Scaffold per navigation stack.
I am new to Android and Jetpack Compose. In iOS you have access to navigationController and can set titles and buttons from all child views. But I can't seem to find a way to do it here. Thanks in advance for any help.
So I found a solution that works. Below is what I did:
1st I have created a custom Top App Bar (I'm adding action button as well)
Also, I have created a NavHost Screen that takes care of the Navigation. This Screen has a ViewModel as well "TopBarViewModel".
So Basically I am changing the Title in VM every time I navigate to said view.