Navigation Arch Component: Passing placeholder param for deeplink

Viewed 2687

I am trying to implement a deeplink functionality using the new Navigation Component API v1.0.0-alpha05 but running into an issue.

Using Android Studio 3.3 Canary 7

Portion of my navigation_graph.xml

<fragment
    android:id="@+id/noteDetailFragment"
    android:name="com.myapp.notes.notedetail.NoteDetailFragment"
    android:label="@string/label_note_detail"
    tools:layout="@layout/note_detail_fragment">

    <argument
        android:name="noteId"
        android:defaultValue="0"
        app:argType="integer" />

    <action
        android:id="@+id/action_noteDetail_to_editNote"
        app:destination="@id/editNoteFragment" />

    <deepLink
        android:id="@+id/noteDetailDeepLink"
        app:uri="notesapp://notes/{noteId}" />
</fragment>

AndroidManifest.xml contains:

    <activity android:name=".presentation.MainActivity">
        <nav-graph android:value="@navigation/navigation_graph" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I am testing my deeplink with adb shell am start -a android.intent.action.VIEW -d "notesapp://notes/2" com.myapp.notes

The noteId is not present in either NoteDetailFragmentArgs.fromBundle(arguments).noteId or arguments?.getInt("noteId", 0) (a default value of 0 is returned in both cases)

Printing out the Bundle shows that it is there:

[{android-support-nav:controller:deepLinkIntent=Intent { act=android.intent.action.VIEW dat=notesapp://notes/2 flg=0x1000c000 pkg=com.mynotes.notes cmp=com.mynotes.notes.presentation.MainActivity }, noteId=2}]

The same issue is observed if the deeplink uri is http://www.mynotes.com/notes/2

How do I access the noteId when deeplinking? Thanks!

3 Answers

As per this issue, arguments parsed from deep links are only added to the arguments Bundle as Strings.

Therefore you should retrieve the noteId via arguments?.getString("noteId")?.toInt() until that issue is fixed.

The Android Navigation Library will always parse placeholder parameters as strings unless the type is explicitly declared in the Nav Graph as an argument.

For a placeholder of {noteId}, receive the passed parameter as an Integer by declaring the argument as an integer.

<argument
    android:name="id"
    app:argType="integer"
    android:defaultValue="0" />
Related