android - 使用导航组件的深层链接未打开我的目标 fragment

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

我在我的应用程序中使用 Android 导航 UI 进行导航。我需要在第二个 fragment 中打开一个网址(动态链接)。

这是我在导航图中的代码

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

并将此导航图添加到 list 中我的主要 Activity

<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 时,我想打开我的第二个 fragment ,但是当我单击此链接时,我的应用程序甚至没有提示在此应用程序中打开。仅提示浏览器。当我单击“使用 Chrome 浏览器打开”时,它只会使用默认启动目的地打开我的应用程序。

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

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

最佳答案

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

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

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

但是as per documentation :

Path parameter placeholders in the form of {placeholder_name} match one or more characters. For example, http://www.example.com/users/{id} matches http://www.example.com/users/4.

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

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

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

关于android - 使用导航组件的深层链接未打开我的目标 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69450854/

相关文章:

android - 添加Firebase云消息传递支持而无需gradle或android studio助手

ios - 两个应用共享相同的通用链接

ios - 深层链接到健康应用程序的医疗 ID 选项卡?

Android导航 - 弹出返回堆栈时删除操作栏后退按钮

android - 是否在 Android 上使用多个 Activity 实例?

android - 在原生 Android 应用程序中使用 WebGL

android - 使用 Android Jetpack 导航时如何禁用导航图标

Android - 在安装和启动时将数据从链接传递到应用程序

android - 使 Android 抽屉导航可滚动

android - 错误 : unable to locate asset entry in pubspec. yaml: "assets/fonts/Lato-Regular.ttf"