I'm trying to navigate using a deep-link for cross-module user navigation, and I need to pass some parameters. Since it's in another module, I don't have access to the id
, so all of the navigate(@IdRes int resId, ...)
methods are off the table.
What's the best way to navigate a deep link with a Uri
and a Bundle
of key-value-pairs using Android Jetpack's Navigation component?
navigation.xml (:app module)
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navGraph"
app:startDestination="@id/feature_one">
<include app:graph="@navigation/feature_one" />
<include app:graph="@navigation/feature_two />
</navigation>
feature_one.xml (:one module)
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/feature_one"
app:startDestination="@id/oneFragment">
<fragment
android:id="@+id/oneFragment"
android:name="my.app.OneFragment"
android:label="OneFragment" />
</navigation>
feature_two.xml (:two module)
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/feature_two"
app:startDestination="@id/twoFragment">
<fragment
android:id="@+id/twoFragment"
android:name="my.app.TwoFragment"
android:label="TwoFragment">
<deepLink app:uri="myapp://my.app/?myId={myId}" />
<argument android:name="myId" app:argType="long" />
</fragment>
</navigation>
OneFragment.kt (:one module)
val bundle = bundleOf("myId" to 123L)
val request = NavDeepLinkRequest.Builder
.fromUri("myapp://my.app?myId={myId}")
.build()
// No place to bundle the args
findNavController().navigate(
request,
navOptions
)
TwoFragment.kt (:two module)
private val args: TwoFragmentArgs by navArgs()
...
val myId: Long = args.myId // never set, so how?