从另一个图表导航到片段,而不是起始目的地 [英] Navigate to a fragment from another graph without it being the start destination

查看:18
本文介绍了从另一个图表导航到片段,而不是起始目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my first graph, I have the following:

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

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

In my second graph I have the following:

<?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",而不是开始目的地,包括第二个图形堆栈

I want to navigate from "notificationFragment" to "detailsFragment" directly without it being the start destination, with including the second graph stack

推荐答案

根据 嵌套图文档:

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

[Nested graphs] also provide a level of encapsulation — destinations outside of the nested graph do not have direct access to any of the destinations within the nested graph.

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

There is one exception to that, when you are navigating using a URI, effectively deep linking into any destination:

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

Unlike navigation using action or destination IDs, you can navigate to any URI in your graph, regardless of whether the destination is visible. You can navigate to a destination on the current graph or a destination on a completely different graph.

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

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

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

Then navigate to that destination via URI:

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

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

It doesn't matter what your URI is, as long as the <deepLink> and what you pass to navigate match. Any arguments you have would need to be encoded in the URL.

这篇关于从另一个图表导航到片段,而不是起始目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆