导航到另一个图的片段,而不将其作为起始目标 [英] navigate to a fragment from another graph without it being the start destination

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

问题描述

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

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)

只要<deepLink>与您传递给navigate的内容匹配,您的URI是什么都没有关系.您拥有的所有参数都需要在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天全站免登陆