hi I'm using navigation compose & my requireme...
# compose
r
hi I'm using navigation compose & my requirement is to have startDestination as list or detail page. Got no issues with list but for detail I need to pass detail id, more in the thread
Copy code
@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"
if i change startDestination to
Copy code
startDestination: String = "$DEST_DETAIL/1"
app is crashing
Copy code
java.lang.IllegalArgumentException: navigation destination 1678697370 is not a direct child of this NavGraph
is this correct way to do it?
i
The
startDestination
needs to match the
route
exactly, so you should be using
"$DEST_DETAIL/{$ARG_DETAIL_ID}"
But you should never, ever be changing the startDestination like that as per the Principles of Navigation: https://developer.android.com/guide/navigation/navigation-principles#fixed_start_destination
Something specifically pointed out in the Navigation Compose guide: https://developer.android.com/jetpack/compose/navigation#create-navhost
What are you trying to accomplish?
r
I'm trying to pass startDestination with id to launch details screen first.
Copy code
startDestination: String = "DEST_DETAIL/{$id}"
some thing like this id will be
Long
i
I think we're having an XY Problem: https://xyproblem.info/
Can you explain your use case from the base level?
r
DetailActivity gets
detail_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?
i can create sample project to explain this scenario
i
I'm confused. What does passing arguments have to do with the startDestination? Changing the start destination is not how you navigate between destinations - you'd want to follow the docs on how to navigate with arguments
i
This gist does not match your initial statement of "my requirement is to have startDestination as list or detail page" at all. Is that original statement actually not the case?
r
ya, sry for the confusion i just need to launch detail composable with the activity's bundle argument.
i
well, then you have two choices: • Add a
deepLink
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 all
🙌 1
r
ty @Ian Lake ill try first approach