使用 Android 导航组件的深层链接打开错误屏幕

ski*_*130 4 android android-intent android-architecture-navigation

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

我在下面分享了我的导航 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>
Run Code Online (Sandbox Code Playgroud)

 

这是清单。

<?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>
Run Code Online (Sandbox Code Playgroud)

 

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

 

老实说,我不确定我哪里出错了,而且现在没有大量关于此的信息,所以我希望从已经修补过这个问题的人那里得到一些建议。我知道设置深层链接的旧方法涉及在清单中我们想要链接到的活动的活动标签下编写意图过滤器。但是在导航组件的框架下,我们现在只有一个主要活动,所有其他屏幕/目的地都是片段。而且由于片段不必(也许不应该/不能?)在清单中注册,我不知道我什至如何与旧方法建立深层链接。

 

所以是的,如果有人能帮助我朝着正确的方向前进,指出我可能犯的任何愚蠢的错误,或者澄清一个误解,我将不胜感激。谢谢你。

ian*_*ake 5

根据这个问题

意图过滤器文档中所述

如果过滤器指定了方案和权限但没有指定路径,则所有具有相同方案和权限的 URI 都匹配,而不管它们的路径如何。

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