如何使用 Android 上的导航控制器从详细信息导航回主屏幕

How to navigate back to the master screen from the details with Nav controller on Android

我有以下 nav_graph xml:

<?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"
    app:startDestination="@id/users_fragment"
    android:id="@+id/nav_graph">

    <fragment
        android:id="@+id/users_fragment"
        android:name="com.package.UsersFragment"
        tools:layout="@layout/users_fragment"
        android:label="UsersFragment">
        <action
            android:id="@+id/action_users_fragment_to_profile_fragment"
            app:destination="@id/profile_fragment"
            app:popUpTo="@id/users_fragment"
            app:popUpToInclusive="true"/>
    </fragment>
    <fragment
        android:id="@+id/profile_fragment"
        android:name="com.package.ProfileFragment"
        android:label="ProfileFragment">
        <argument
            android:name="userId"
            app:argType="string" />
    </fragment>
</navigation>

主要activityxml是:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="viewModel"
            type="com.package.UsersViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:fitsSystemWindows="true"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:background="@color/colorPrimary" />

        </com.google.android.material.appbar.AppBarLayout>

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/app_bar_layout"
            app:navGraph="@navigation/nav_graph" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

要导航到我调用的详细信息:

val action = UsersFragmentDirections
    .actionUsersFragmentToProfileFragment(
        userId = user.userId,
    )
navController.navigate(action)

其中 UsersFragmentDirections 由 safe-args 插件生成。

问题是当我按回主片段时没有显示:activity 已关闭。 如果在操作中我使用 app:popUpToInclusive="false" 则后退按钮不再起作用。 当用户按回键时,我应该怎么做才能从 ProfileFragment 转到 UsersFragment?

您在移动到配置文件片段时弹出用户片段。因此,当您按回键时,后台堆栈中没有任何内容,因此 activity 完成.. 因此,要解决此问题,您有两种选择: 1) 手动 re-open/navigate 用户片段 要么 2) 去掉这两行

app:popUpTo="@id/users_fragment"
app:popUpToInclusive="true"