gpt4 book ai didi

android - 如何使用导航组件切换到不同后台堆栈中的其他 fragment ?

转载 作者:行者123 更新时间:2023-12-03 13:27:27 24 4
gpt4 key购买 nike

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

  • Home , 我有 Fragment1Fragment2 ,
  • Dashboard , 我有 Fragment3Fragment4
  • Profile , 我有 MyProfileEditProfile .

  • 现在,在 Fragment2 , 一个按钮 changeAvatar可以打开 EditProfile在堆栈中 Profile .因为 EditProfile应该在标签 Profile 中,所以如果我不想包含 EditProfile进入 navGraphHome ,我怎样才能实现这种行为?

    最佳答案

    您正在寻找的是 global action .

    鉴于您有以下 nav_graph结构体:

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

    备注 action actionProfile 内的部分:
    <action
    android:id="@+id/navigateToEditProfile"
    app:destination="@id/editProfileFragment" />

    以上为实际 全局行动你正在寻找的。

    因此,要透视流程,您将执行以下操作以从 Fragment2 导航。更改头像按钮。

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

    关于android - 如何使用导航组件切换到不同后台堆栈中的其他 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61498100/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com