Hi, I have an issue about Navigation component. He...
# compose
e
Hi, I have an issue about Navigation component. Here is my navigation implementation:
@Composable
_fun_ MainScreen() {
_val_ navController = rememberNavController()
Navigation(navController = navController)
}
@Composable
_fun_ Navigation(
navController: NavHostController
) {
NavHost(
navController = navController,
startDestination = _homeRoute_,
enterTransition = *{* EnterTransition.None *}*,
exitTransition = *{* ExitTransition.None *}*,
popEnterTransition = *{* EnterTransition.None *}*,
popExitTransition = *{* ExitTransition.None *}*
) *{*
_homeRoute_(
navigateToDetail = *{* deepLink *->*
navController._navigateToDetail_(deepLink)
}
)
_detailRoute_(
onBackPressed = *{* navController.popBackStack() *}*
)
}
}
_fun_ NavGraphBuilder.homeRoute(
navigateToDetail: (String) -> Unit
) {
_composable_(_homeRoute_) *{*
HomeRoute(navigateToDetail = navigateToDetail)
}
}
_fun_ NavGraphBuilder.detailRoute(onBackPressed: () -> Unit) {
_composable_(
route = _detailRouteWithArg_,
arguments = _listOf_(
_navArgument_(_pairArg_) *{*
type = NavType.StringType
}
)
) *{*
*it*.arguments?.getString(_pairArg_)?._let_ *{*
DetailRoute(onBackPressed = onBackPressed)
}
}
}
_fun_ NavController.navigateToDetail(deepLink: String) {
navigate("$_detailRoute_/${deepLink._encode_()}")
}
@Composable
_internal fun_ HomeRoute(
viewModel: HomeViewModel = hiltViewModel(),
navigateToDetail: (String) -> Unit
)
@Composable
_internal fun_ DetailRoute(
onBackPressed: () -> Unit,
viewModel: DetailViewModel = hiltViewModel()
)
In this case, why NavHost, AnimatedContent, LocalOwnersProvider, SaveableStateProvider keeps recomposition multiple times
🧵 4
a
Is MainScreen the only parent composable, or are you using a Scaffold with a scrollable TopBar + consuming innerPadding above it? If that's the case, it's just a known drawback of Scaffold. You could try implementing it yourself, without Scaffold. For simple layouts this should be easy. For example, I had a layout with scrollable TopBar, fixed BottomBar, SnackbarHost, and actual content. As long as TopBar was collpasing, I was seeing recompositions of NavHost, which drilled down to child composables too. Replacing it with my own layout fixed that issue completely, but I still see exactly 3 recompositions of NavHost's *Providers. Don't know why, maybe it's unavoidable.
e
there is no scaffold here, it is just called by MainActivity like this:
_setContent_ *{*
MyTheme *{*
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.
_fillMaxSize_(),
color = MaterialTheme.colorScheme.background )
{
MainScreen()
}
}
}
a
How many times does it recompose, then? Is it 3, same as mine? Could be useful if you post what Layout Inspector shows.
e
Yes it goes like 3n
my detail route called 3 times for every navigation