简体   繁体   English

有没有办法更新 Jetpack Navigation Graph 中的任何 Fragment 都可以访问的 Fragment?

[英]Is there a way to update Jetpack Navigation Graph for a Fragment that can be accessible by any Fragment in the Graph?

Given I have a Jetpack Navigation Graph as below, where by鉴于我有一个如下所示的 Jetpack 导航图,其中

BlankFragment1 -> BlankFragment2 -> BlankFragment3 -> BlankFragment4 -> BlankFragment5 BlankFragment1 -> BlankFragment2 -> BlankFragment3 -> BlankFragment4 -> BlankFragment5

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:startDestination="@id/blankFragment1">
    <fragment
        android:id="@+id/blankFragment1"
        android:name="com.example.cashdog.cashdog.BlankFragment1"
        android:label="@string/label_blank1"
        tools:layout="@layout/fragment_blank1" >
        <action
            android:id="@+id/action_blankFragment1_to_blankFragment2"
            app:destination="@id/blankFragment2" />
    </fragment>
    <fragment
        android:id="@+id/blankFragment2"
        android:name="com.example.cashdog.cashdog.BlankFragment2"
        android:label="@string/label_blank2"
        tools:layout="@layout/fragment_blank2" >
        <action
            android:id="@+id/action_blankFragment2_to_blankFragment3"
            app:destination="@id/blankFragment3" />
    </fragment>
    <fragment
        android:id="@+id/blankFragment3"
        android:name="com.example.cashdog.cashdog.BlankFragment3"
        android:label="@string/label_blank3"
        tools:layout="@layout/fragment_blank3" >
        <action
            android:id="@+id/action_blankFragment3_to_blankFragment4"
            app:destination="@id/blankFragment4" />
    </fragment>
    <fragment
        android:id="@+id/blankFragment4"
        android:name="com.example.cashdog.cashdog.BlankFragment4"
        android:label="@string/label_blank4"
        tools:layout="@layout/fragment_blank4" >
        <action
            android:id="@+id/action_blankFragment4_to_blankFragment5"
            app:destination="@id/blankFragment5" />
    </fragment>
    <fragment
        android:id="@+id/blankFragment5"
        android:name="com.example.cashdog.cashdog.BlankFragment5"
        android:label="@string/label_blank5"
        tools:layout="@layout/fragment_blank5" />

</navigation>

If I have another Fragment, named FragmentSomething, that can be open by and of the FragmentBlank1 to FragentBlank5如果我有另一个 Fragment,名为 FragmentSomething,它可以由 FragmentBlank1 和 FragmentBlank5 打开

Is there a way to update the NavGraph above for my FragmentSomething, without the need to add actions for all 5 of them?有没有办法为我的 FragmentSomething 更新上面的 NavGraph,而无需为所有 5 个添加操作?

Apparently, there's something called the global action显然,有一种叫做全球行动的东西

https://developer.android.com/guide/navigation/navigation-global-action https://developer.android.com/guide/navigation/navigation-global-action

We just need to add this我们只需要添加这个

    <action android:id="@+id/action_global_somethingFragment"
          app:destination="@id/somethingFragment"/>


    <fragment
        android:id="@+id/somethingFragment"
        android:name="com.example.cashdog.cashdog.SomethingFragment"
        android:label="@string/label_something"
        tools:layout="@layout/fragment_something" />

Or we can navigate the code directly to somethingFragment using或者我们可以使用直接将代码导航到somethingFragment

findNavController().navigate(R.id.somethingFragment)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM