【问题标题】:Deep Link Using Android Navigation Component Opens Incorrect Screen使用 Android 导航组件的深层链接打开不正确的屏幕
【发布时间】:2020-03-05 12:32:02
【问题描述】:

您好,我正在学习如何使用 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-graph 标记并将其指向正确的导航图文件。如果我正确设置了这些东西,我应该设置一个正确的深层链接并且可以开始使用。问题是,当我输入以下命令来测试深层链接adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos/" 时,它会打开默认活动,或者更确切地说是默认目标(与我在这里设置的所有其他目标一样的片段)。

 

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

 

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

【问题讨论】:

  • 如果你使用app:uri="example://gizmos/new",它是否有效?听起来您正在点击 this issue,因为您使用的是带有方案和主机的 URI,但没有路径。
  • 是的,做到了!非常感谢!

标签: android android-intent android-architecture-navigation


【解决方案1】:

根据this issue

intent filter documentation中提到的:

如果过滤器指定了方案和权限,但没有指定路径,则具有相同方案和权限的所有 URI 都匹配,无论其路径如何。

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

【讨论】:

  • 您也可以将uri更改为example:/gizmos。基本上你不能这样做 example://gizmos 因为它认为 gizmos 是权威(这是可选的)然后找不到路径(这在 uri 中是强制性的)。
  • @ianhanniballake 我们的过滤器指定了方案和权限,但没有路径。但是当我将这个 uri:myapp://settings 添加到我的应用程序时,清单合并也会将 添加到意图过滤器中。这样, myapp://settings 无法处理,但 myapp://settings/ 。有没有办法从我的过滤器中删除路径?
  • @osrl - 在 Android 上,无法编写只匹配空的、没有 / 路径 URI 的意图过滤器 - 没有 android:path每个 路径根据这个答案进行匹配,这不是您的深层链接所说的匹配。
  • 当我们没有使用导航组件时,我们只在我们的意图过滤器中添加了android:scheme,并手动处理了权限。似乎没有办法自动使用导航组件。导航组件如何处理隐式深层链接?有没有办法在不从导航图中删除深层链接标签的情况下覆盖其行为?
猜你喜欢
  • 2021-07-04
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2020-09-09
  • 1970-01-01
相关资源
最近更新 更多