繁体   English   中英

使用 Android 导航组件的深层链接打开不正确的屏幕

[英]Deep Link Using Android Navigation Component Opens Incorrect Screen

嗨,我正在学习如何使用 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>

这是清单。

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

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

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

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

根据这个问题

意图过滤器文档中所述:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM