Ravi
05/17/2021, 11:00 PMRavi
05/17/2021, 11:00 PM@Composable
fun NavGraph(startDestination: String = DEST_LIST) {
val navController = rememberNavController()
NavHost(navController, startDestination = startDestination) {
composable(
route = DEST_LIST,
) {
Column {
repeat(5) {
Text(text = "List Item : $it")
}
}
}
composable(
route = "$DEST_DETAIL/{$ARG_DETAIL_ID}",
arguments = listOf(navArgument(ARG_DETAIL_ID) { type = NavType.LongType })
) {
// Load Detail
Text(text = "ARG_DETAIL_ID: ${it.arguments?.getLong(ARG_DETAIL_ID)}")
}
}
}
const val DEST_LIST = "listDestination"
const val DEST_DETAIL = "detailDestination"
const val ARG_DETAIL_ID = "detailId"
Ravi
05/17/2021, 11:02 PMstartDestination: String = "$DEST_DETAIL/1"
app is crashing
java.lang.IllegalArgumentException: navigation destination 1678697370 is not a direct child of this NavGraph
Ravi
05/17/2021, 11:02 PMIan Lake
05/17/2021, 11:07 PMstartDestination
needs to match the route
exactly, so you should be using "$DEST_DETAIL/{$ARG_DETAIL_ID}"
Ian Lake
05/17/2021, 11:08 PMIan Lake
05/17/2021, 11:08 PMIan Lake
05/17/2021, 11:09 PMRavi
05/17/2021, 11:10 PMstartDestination: String = "DEST_DETAIL/{$id}"
some thing like this id will be Long
Ian Lake
05/17/2021, 11:11 PMIan Lake
05/17/2021, 11:13 PMRavi
05/17/2021, 11:16 PMdetail_id
as argument and this particular activity got 3-4 composables under navgraph. I've to pass this detail_id
as startDestination to launch detail composable. If I'm using fragments i get these args through navArgs
. Is there similar mechanism for composables navgraph?Ravi
05/17/2021, 11:18 PMIan Lake
05/17/2021, 11:18 PMRavi
05/17/2021, 11:58 PMIan Lake
05/18/2021, 12:10 AMRavi
05/18/2021, 12:13 AMIan Lake
05/18/2021, 12:18 AMdeepLink
to your destination. NavController will automatically check your Intent
for a matching deep link (looking at the data
field of the Intent) and parse arguments out of that deeplink, starting that destination with the correct arguments. This is my recommendation
• Set a defaultValue
on your navArgument
, not touching the startDestination
at allRavi
05/18/2021, 12:19 AM