【问题标题】:Deep Link using Navigation Component not opening my destination fragment使用导航组件的深层链接未打开我的目标片段
【发布时间】:2021-10-05 12:54:03
【问题描述】:

我正在使用 Android Navigation UI 在我的应用中进行导航。我需要在我的第二个片段中打开一个 Web URL(动态链接)。

这是我在导航图中的代码

<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/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.myproject.main.HomeFragment"
        android:label="Home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_nav_home_to_secondFragment"
            app:destination="@id/secondFragment" />

    </fragment>
    <fragment
        android:id="@+id/nav_library"
        android:name="com.myproject.main.SecondFragment"
        android:label="@string/menu_library"
        tools:layout="@layout/fragment_library">
       <deepLink
            android:id="@+id/deepLink"
            app:uri="https://myproject.page.link/*" />

    </fragment>
</navigation>

并将此导航图添加到清单中的主要活动中

<activity
     android:name=".main.MainActivity"
     android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
     android:label="@string/app_name"
     android:theme="@style/AppTheme.NoActionBar"
      >
     <nav-graph android:value="@navigation/mobile_navigation" />
</activity> 

当用户单击 https://myproject.page.link/i5sA 之类的 URL 时,我想打开我的第二个片段,但是当我单击此链接时,我的应用程序甚至没有被提示在此应用程序中打开。仅提示浏览器。当我点击使用 chrome 浏览器打开时,它只会使用默认的起始目的地打开我的应用程序。

我需要的是,当用户按下该网页 URL 时,我需要使用 SecondFragment 打开我的应用 MainActivity 并获取该 URL 数据,而不是打开默认的主页片段。

我看过developer page 和很多文章和教程,但我不知道我在这里缺少什么,任何人都可以告诉我...

【问题讨论】:

    标签: android deep-linking android-navigation android-navigation-graph


    【解决方案1】:

    app:uri="https://myproject.page.link/*"

    使用* 作为占位符在 android 架构导航中不是有效的深层链接。

    虽然https://myproject.page.link/* URI 可以完全按原样打开https://myproject.page.link/*(即带有星号)

    但是as per documentation:

    {placeholder_name} 形式的路径参数占位符匹配一个或多个字符。例如,http://www.example.com/users/{id} 匹配 http://www.example.com/users/4

    因此,如果您想将此星号作为占位符,以便将某些信息传递给您的应用程序,您需要将其包裹在花括号 {} 中;所以你的 URI 是:

    app:uri="https://myproject.page.link/{placeholder_name}"

    请注意,您可以从 Google 搜索应用程序(而不是浏览器应用程序)测试此 URI,因为如果您想从浏览器打开它,您必须全局注册域名;这是为了让 Google 验证您是 URI 的所有者。 Check here for more info.

    【讨论】:

    • 如果我使用{data} 而不是*,我的片段是否会使用默认FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent()) 处理该动态链接?
    • @Venkat .. This post 可以帮助你
    • this post 他说在另一个活动中处理深层链接并在任务成功打开主要活动后。如何使用其他链接处理程序活动中加载的数据打开主要活动目标片段?
    • @Venkat 你能用相关的firebase标签打开另一个问题,以便有人可以帮助你吗?这也超出了这个问题的范围;但到目前为止,除了答案中提到的之外,在导航组件中没有其他合法的方法可以做到这一点。
    猜你喜欢
    • 2020-03-05
    • 2021-07-04
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多