gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:37:14 39 4
gpt4 key购买 nike

我正在创建一个简单的 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 whenthe user presses the "Up" button marked with a left (or start)-facingarrow in the upper left (or starting) corner of the app UI.

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



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

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

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