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

标签 android android-intent android-architecture-navigation

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

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

这是导航文件,相关位是最后一个创造性地命名为“firstDeepLinkFragment”的 fragment 标签。


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

这是 list 。

<?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 建立为该标签的属性,然后添加 list 中的导航图标记并将其指向正确的导航图文件。如果我把这些东西设置正确,我应该有一个正确的深度链接设置并且可以开始了。问题是,当我输入以下命令来测试深层链接时 adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos/" 它反而打开了默认 Activity ,或者更确切地说是默认目的地(这是一个 fragment ,就像我在这里设置的所有其他目的地一样)。

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

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

最佳答案

根据 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 是权限,但您缺少路径部分。通过更改您的深层链接以同时包含路径,Navigation 将正确地将您的深层链接与目标匹配。

关于android - 使用 Android 导航组件的深层链接打开不正确的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58773944/

相关文章:

android - 缺少 Visual Studio 2015 所需的包

android - 找不到 Gradle DSL 方法 : 'compileSdkVersion()' after importing a module into the project

java - 在Android Studio中编译时出现"not an enclosing class"错误

Android Jetpack Navigation 如何处理 Toolbar 和 BottomNavBar 内容

android - 从导航的深层链接调用我的应用程序会导致我的 fragment 被多次实例化

android - 喷气背包导航 : Hide label in toolbar

Android:如何使任务管理器和菜单中的应用程序不可见

java - onValueSelected() 没有在 BarChart 中调用,而是 onNothingSelected() 每次当我点击 BarChart 中的任何栏或任何地方时调用

android - ActionBar 菜单项的不同行为

android - 如果应用程序在后台,如何不启动 Activity