android - navigateUp() 和 popBackStack() 的区别

标签 android android-studio kotlin navigation

我正在创建一个简单的 ToDo 应用程序并检查导航方法以从 AddTask fragment 返回到我的 mainFragment。我发现我可以使用 navigateUp() 和 popBackStack() 返回,但我不明白其中的区别。
当我使用这个时:

Navigation.findNavController(it).navigateUp()
或这个:
Navigation.findNavController(it).popBackStack()
我回到 mainFragment,我认为 addTaskFragment 是从堆栈中弹出的,所以有人可以解释一下吗?

最佳答案

抱歉,(编辑:以前)接受的答案是绝对错误的,navigateUp()如果有的话,也会从 backstack 中弹出顶部的 fragment 。
初学者:如果您从应用程序中到达当前目的地,它们的行为完全相同。
仅当您使用来自不同应用的深层链接到达当前目的地时,它们的行为才会有所不同:navigateUp()将离开您的应用并返回到导航到您应用中的深层链接的应用。popBackStack()将尝试在您的 backstack 中返回一步,如果没有 backstack 条目,则不会执行任何操作。
您可以阅读以下内容的差异:
https://developer.android.com/reference/androidx/navigation/NavController#navigateUp()

navigateUp() Attempts to navigate up in the navigation hierarchy. Suitable for when the user presses the "Up" button marked with a left (or start)-facing arrow in the upper left (or starting) corner of the app UI.

The intended behavior of Up differs from Back when the user did not reach the current destination from the application's own task. e.g. if the user is viewing a document or link in the current app in an activity hosted on another app's task where the user clicked the link. In this case the current activity (determined by the context used to create this NavController) will be finished and the user will be taken to an appropriate destination in this app on its own task.



popBackStack() Attempts to pop the controller's back stack. Analogous to when the user presses the system Back button when the associated navigation host has focus.

关于android - navigateUp() 和 popBackStack() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67245601/

相关文章:

android - HTC Desire S 的 Ubuntu 中缺少 ADB?

android - 如何检查数据是否插入到房间数据库中

android - 如何在 Kotlin 中传递方法接受参数?

java - 无法解析 : Lcom/google/common/collect/ImmutableMap; at com. my.app.di.DaggerMyApplicationComponent.getMapOfClassOfAndProviderOfFactoryOf(

android - android studio 调试问题

node.js - 如何在 Kotlin-React-App 中导入 Bootstrap 库

android - RuntimeException + ConnectException = Android 应用程序崩溃 (KSOAP2)

android - 如何让 Edittext(多行)在顶部位置可绘制

android - 我对我必须做什么感到困惑(android 渲染引擎)

dependencies - 将本地库项目添加为 Android Studio 中多个项目的依赖项