Sterling Albury
06/05/2022, 11:45 PMfun SomeComposableContent(startRoute: String) =
NavHost(
route = "outer",
startDestination = "inner/{userId}"
) {
navigation(startDestination = "inner/start/{userId}", route = "inner/{userId}" {
composable(route = "inner/start/{userId}") { backEntry ->
val userId = backEntry.arguments?.getString("userId")
ScreenOne(userId)
}
}
composable(route = "next") {
ScreenTwo()
}
}
If I use the "next" destination as my start and then navigate to the inner graph with passing an arg, things work, but if I want to start at the inner start destination, and I'm not calling navController.navigate(), how can I pass the argument? I'm basically trying to load the graph and pass the argument..is that possible?Ian Lake
06/06/2022, 12:43 AMSterling Albury
06/06/2022, 2:30 AMIan Lake
06/06/2022, 4:01 AM{userId}
is still going to be automatically parsed, using the defaultValue
by defaultwhatever/{param1}/{param2}
is perfect if they are required arguments, otherwise you'd want to use the query parameter syntax as per the docs: https://developer.android.com/jetpack/compose/navigation#optional-argsSterling Albury
06/06/2022, 4:12 AMIan Lake
06/06/2022, 5:47 AMSterling Albury
06/06/2022, 5:55 PMYou're not parsing anything; that's done for you. You're just getting the pre-parsed value out of the Bundle.if I want to set the default value when I first load the graph, then I will have to parse that value myself, right? that part feels weird to me...i'd rather not have to parse anything and somehow have the nav controller know there's an argument.
Ian Lake
06/07/2022, 12:10 AMSterling Albury
06/07/2022, 7:33 PMNavHost() {
composable(route = "") {
NavHost() {
navigation() {
composable(route =""){
}
}
}
}
}
Ian Lake
06/07/2022, 10:28 PMTherefore, the recommendation for hybrid apps is to use the fragment-based Navigation component and use fragments to hold view-based screens, Compose screens, and screens that use both views and Compose. Once each screen fragment in your app is a wrapper around a composable, the next step is to tie all of those screens together with Navigation Compose and remove all of the fragments.If you are in a fragment world, you should stay in a fragment world, even if your entire fragment's UI is built with Compose
Sterling Albury
06/07/2022, 11:22 PMBerkeli Alashov
03/03/2023, 12:07 AMMyComposeFeatureScreen:
rememberNavController()
FeatureScreenOne
FeatureScreenTwo
// Navigation backstack not restored
MainActivity {
onCreate:
jetpack fragment navigation:
fragment:
setContent MyComposeFeatureScreen:
}
// Navigation backstack restored
MainActivity {
onCreate:
setContent MyComposeFeatureScreen:
}
Diff in logs when coming back from process death:
CustomersLaunchedEffect: navController: Destination(0x4998a1c7) route=customers/, androidx.navigation.NavBackStackEntry@198f9b8
CustomersLaunchedEffect: customerActionResult: MutableState(value=null)@120014880
CustomersFragment: LaunchedEffect
CustomersLaunchedEffect: navController: Destination(0x9acde377) route=customers/{customerId}?isEditMode={isEditMode}&isInvoiceMode={isInvoiceMode}, androidx.navigation.NavBackStackEntry@73d12977
CustomersLaunchedEffect: customerActionResult: MutableState(value=null)@50352885
CustomersFragment: LaunchedEffect
CustomersLaunchedEffect: navController: Destination(0x4998a1c7) route=customers/, androidx.navigation.NavBackStackEntry@e2b2011e
CustomersLaunchedEffect: customerActionResult: MutableState(value=null)@80460568
CustomersLaunchedEffect: navController: Destination(0x4998a1c7) route=customers/, androidx.navigation.NavBackStackEntry@70ec0fac
CustomersLaunchedEffect: customerActionResult: MutableState(value=null)@101095983
So is the way we're trying to use compose NavHostController in Fragment is not right / supported? Or is it a bug in rememberNavController?Ian Lake
03/03/2023, 12:24 AMrememberNavController
doesn't work but other things that rely on rememberSaveable
do work, so that would be the first thing I'd try (just having a rememberSaveable
integer that a button increments and seeing if that count works across process death). If neither work, then your issue is with your Fragment codeBerkeli Alashov
03/03/2023, 12:39 AM