Reputation: 313
Hi I'm learning how to use Android's Navigation Component and as part of that I'm making a very basic practice app to try stuff out. I've got basic navigation and data transfer between two screens set up but the part I'm stuck on is setting up a deep link. Currently I've put a deep link tag in a destination (fragment) that currently can't be reached in any other way. This is basically a third screen separate from the other two connected screens and the deep link is the only way to access it.
I've shared my navigation xml file below as well as my manifest.
Here is the navigation file, the relevant bit is the last fragment tag creatively named "firstDeepLinkFragment".
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation_graph"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="android.bignerdranch.navcontrollertest.FirstFragment"
android:label="navigation_first_fragment"
tools:layout="@layout/navigation_first_fragment" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:enterAnim="@anim/nav_default_enter_anim"/>
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="android.bignerdranch.navcontrollertest.SecondFragment"
android:label="navigation_second_fragment"
tools:layout="@layout/navigation_second_fragment" />
<fragment
android:id="@+id/firstDeepLinkFragment"
android:name="android.bignerdranch.navcontrollertest.FirstDeepLinkFragment"
android:label="first_deeplink_fragment"
tools:layout="@layout/first_deeplink_fragment" >
<deepLink
android:id="@+id/deepLink"
app:uri="example://gizmos" />
</fragment>
</navigation>
Here's the manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.bignerdranch.navcontrollertest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<nav-graph android:value="@navigation/navigation_graph"/>
</activity>
</application>
</manifest>
So from my understanding of how deep links work under the Navigation Component, all I have to do is add the deep link tag to the destination I want to link to, establish the URI as an attribute of that tag, and then add the nav-graph tag in the manifest and point it to the right navigation graph file. If I set that stuff up right I should have a proper deep link set up and good to go. The problem is that when I enter the following command to test the deep link adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos/"
it instead opens the default activity, or rather the default destination (which is a fragment like every other destination I've set up here).
I'm honestly not sure where I may have gone wrong and there isn't a ton of information out about this right now so I was hoping for some advice from people who have already tinkered around with this. I know the old way to set up deep links involved writing intent filters in the manifest under the activity tag of the activity we wanted to link to. But under the framework of the Navigation Component we now only have one main activity and all our other screens/destinations are fragments. And since fragments don't have to be (and maybe shouldn't/can't be?) registered in the manifest I don't know how I could even set up a deep link with the old methodology.
So yeah if anyone could help steer me in the right direction, point out any silly mistake I may have made, or clear up a misunderstanding I would really appreciate it. Thank you.
Upvotes: 4
Views: 5288
Reputation: 199805
As per this issue,
As mentioned in the intent filter documentation:
If a filter specifies a scheme and an authority but no path, all URIs with the same scheme and authority match, regardless of their paths.
When you use app:uri="example://gizmos"
, the example://
is the scheme and gizmos
is the authority, but you're missing the path part. By changing your deep links to include a path as well, Navigation will correctly match your deep link to the destination.
Upvotes: 6