【发布时间】: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