24

在我的第一张图中,我有以下内容:

<?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/firstGraph"
    app:startDestination="@id/listFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="com.example.ListFragment">

        <action
            android:id="@+id/action_list_to_details"
            app:destination="@id/detailsFragment" />

    </fragment>

    <fragment
        android:id="@+id/detailsFragment"
        android:name="com.example.DetailsFragment">

    </fragment>
</navigation>

在我的第二张图中,我有以下内容:

<?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/secondGraph"
    app:startDestination="@id/dashboardFragment">

    <include app:graph="@navigation/firstGraph" />

    <fragment
        android:id="@+id/dashboardFragment"
        android:name="com.example.DashboardFragment">
        <action
            android:id="@+id/action_dashboard_to_notification"
            app:destination="@id/notificationFragment"/>
    </fragment>

    <fragment
        android:id="@+id/notificationFragment"
        android:name="com.example.NotificationsFragment">

        <action
            android:id="@+id/action_notification_to_details"
            app:destination="@id/firstGraph"/>

    </fragment>
</navigation>

我想直接从“notificationFragment”导航到“detailsFragment”而不是开始目的地,包括第二个图形堆栈

4

1 回答 1

33

根据嵌套图文档

[嵌套图] 还提供了一定程度的封装——嵌套图之外的目的地无法直接访问嵌套图中的任何目的地。

有一个例外,当您使用 URI 导航时,有效地深度链接到任何目的地:

与使用操作或目标 ID 的导航不同,您可以导航到图表中的任何 URI,而不管目标是否可见。您可以导航到当前图表上的目的地或完全不同图表上的目的地。

因此,您可以向图表添加隐式深层链接

<fragment
    android:id="@+id/detailsFragment"
    android:name="com.example.DetailsFragment">
    <deepLink app:uri="android-app://your.package.name/details" />
</fragment>

然后通过 URI 导航到该目的地:

val uri = Uri.parse("android-app://your.package.name/details")
navController.navigate(uri)

你的 URI 是什么并不重要,只要<deepLink>和你传递的内容相navigate匹配。您拥有的任何参数都需要在 URL 中进行编码。

于 2020-02-16T04:03:31.193 回答