本文介绍了如何使用导航组件切换到不同后台堆栈中的其他片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个底部导航选项卡,分别称为 Home、Dashboard、Profile.

I have 3 bottom navigation tabs called Home, Dashboard, Profile.

  • Home,我有Fragment1Fragment2
  • Dashboard 中,我有 Fragment3Fragment4
  • Profile 中,我有 MyProfileEditProfile.
  • In Home, I have Fragment1 and Fragment2,
  • In Dashboard, I have Fragment3 and Fragment4
  • And in Profile, I have MyProfile and EditProfile.

现在,在 Fragment2 中,一个按钮 changeAvatar 可以打开堆栈 Profile 中的 EditProfile.因为 EditProfile 应该在标签 Profile 中,所以如果我不想将 EditProfile 包含到 navGraphHome,我该如何实现这种行为?

Now, in Fragment2, a button changeAvatar can open EditProfile in stack Profile. Because EditProfile should be in tab Profile, so if I don't want to include EditProfile into navGraph of Home, how can I achieve that behavior?

推荐答案

你要找的就是 全局操作.

假设您有以下 nav_graph 结构:

Given you have the following nav_graph structure:

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

    <navigation
            android:id="@+id/actionHome"
            android:label="Home"
            app:startDestination="@id/fragment1">
        <fragment
                android:id="@+id/fragment1"
                android:name="com.example.app.Fragment1"
                android:label="Home Fragment 1"
                tools:layout="@layout/fragment_1" />
        <fragment
                android:id="@+id/fragment2"
                android:name="com.example.app.Fragment2"
                android:label="Home Fragment 2"
                tools:layout="@layout/fragment_2" />

    </navigation>

    <navigation
            android:id="@+id/actionDashboard"
            android:label="Dashboard"
            app:startDestination="@id/fragment3">
        <fragment
                android:id="@+id/fragment3"
                android:name="com.example.app.Fragment3"
                android:label="Dashboard Fragment 3"
                tools:layout="@layout/fragment_3" />
        <fragment
                android:id="@+id/fragment4"
                android:name="com.example.app.Fragment4"
                android:label="Dashboard Fragment 4"
                tools:layout="@layout/fragment_4" />

    </navigation>

    <navigation
            android:id="@+id/actionProfile"
            android:label="Profile"
            app:startDestination="@id/myProfileFragment">
        <fragment
                android:id="@+id/myProfileFragment"
                android:name="com.example.app.MyProfileFragment"
                android:label="My Profile"
                tools:layout="@layout/fragment_my_profile"/>

        <fragment
                android:id="@+id/editProfileFragment"
                android:name="com.example.app.EditProfileFragment"
                android:label="Edit Profile"
                tools:layout="@layout/fragment_edit_profile"/>

        <action
                android:id="@+id/navigateToEditProfile"
                app:destination="@id/editProfileFragment" />
    </navigation>
</navigation>

注意 actionProfile 中的action 部分:

<action
        android:id="@+id/navigateToEditProfile"
        app:destination="@id/editProfileFragment" />

以上是您正在寻找的实际全局操作.

The above is the actual global action that you are looking for.

因此,要透视流程,您将执行以下操作以从 Fragment2 changeAvatar 按钮导航.

So to put the flow into perspective you would do the following to navigate from Fragment2 changeAvatar button.

fun navigateToChangeAvatar() {
    changeAvatar.setOnClickListener { view ->
        view.findNavController().navigate(R.id.navigateToEditProfile)
    }
}

这篇关于如何使用导航组件切换到不同后台堆栈中的其他片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 03:08