深度 Link 使用 Android 导航组件打开不正确的屏幕

Deep Link Using Android Navigation Component Opens Incorrect Screen

您好,我正在学习如何使用 Android 的导航组件,作为其中的一部分,我正在制作一个非常基本的练习应用程序来尝试一些东西。我已经设置了两个屏幕之间的基本导航和数据传输,但我坚持的部分是设置深度 link。目前,我在目前无法通过任何其他方式到达的目的地(片段)中放置了一个很深的 link 标记。这基本上是与其他两个连接的屏幕分开的第三个屏幕,深度 link 是访问它的唯一方法。

我在下面分享了我的导航 xml 文件以及我的清单。

这是导航文件,相关位是最后一个片段标签创造性地命名为"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>

这是清单。

<?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>

因此,根据我对 links 在导航组件下工作的深度的理解,我所要做的就是将深度 link 标记添加到我想要 link 的目的地to,建立URI作为那个标签的属性,然后在manifest中添加nav-graph标签,指向正确的导航图文件。如果我把这些东西设置正确,我应该有一个适当的深度 link 设置并且可以开始了。问题是,当我输入以下命令来测试深度 link adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos/" 时,它会打开默认的 activity,或者更确切地说是默认的目的地(这是一个像我其他目的地一样的片段在这里设置)。

老实说,我不确定哪里出了问题,而且目前还没有大量关于这方面的信息,所以我希望能从已经对此进行过修补的人那里得到一些建议。我知道设置深度 links 的旧方法涉及在我们想要 link 的 activity 的 activity 标签下的清单中编写意图过滤器。但是在Navigation Component的框架下,我们现在只有一个mainactivity,其他的screens/destinations都是fragments。而且由于片段不必(也许不应该t/can不?)在清单中注册,我不知道如何使用旧方法设置深度 link .

所以是的,如果有人能帮助我指引正确的方向,指出我可能犯的任何愚蠢错误,或者消除误解,我将非常感激。谢谢。

根据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.

当您使用 app:uri="example://gizmos" 时,example:// 是方案,gizmos 是权限,但是您缺少路径部分。通过更改您的深度 link 以包含路径,导航将正确地将您的深度 link 与目的地相匹配。