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

问题描述 投票:0回答:1

我有以下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>

主要活动xml是:

<?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插件生成。

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

我具有以下nav_graph xml:

android android-architecture-components master-detail
1个回答
0
投票

您正在移动用户片段,而移至个人资料片段。因此,当您按回退键时,Backstack中没有任何内容,因此活动结束。因此,要解决此问题,您有两种选择:1)手动重新打开/导航用户片段要么2)删除这两行

© www.soinside.com 2019 - 2024. All rights reserved.