android - 如何使用 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>
主要活动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)
whereUsersFragmentDirections
由 safe-args 插件生成。
问题是当我按下主片段时没有显示:活动已关闭。如果在我使用的操作中,app:popUpToInclusive="false"
则后退按钮不再起作用。当用户按下返回时,我应该怎么做才能从 ProfileFragment 转到 UsersFragment?
解决方案
您在移动到个人资料片段时弹出用户片段。因此,当您按下返回时,后台堆栈中没有任何内容,因此活动完成。因此,要解决此问题,您有两个选择:1)手动重新打开/导航用户片段或 2)删除这两行
app:popUpTo="@id/users_fragment"
app:popUpToInclusive="true"
推荐阅读
- java - 方法不会覆盖或实现超类型 ExoPlayer 中的方法
- firebase - Firebase 安全规则 newData 不起作用
- cucumber - 标签过滤系统 - Cucumber
- node.js - 在“npm start”上使用 WSL + Electron + Typescript 获取 SUID Sandbox Helper 错误
- html - CSS 网格日历 - 事件时间跨度
- raku - 如何打印一个对象,输入 nqp
- angular - 由于文件名错误,Angular 未下载
- authentication - 有没有办法让用户在 Hashicorp 的 Vault UI 上更改自己的密码
- c - '"./testing"' 不是内部或外部命令、可运行程序或批处理文件
- css - 如何使用 CSS 获得正方形中的正方形?