问题描述
当前在我的导航图中,我有对话框片段目标(Fragment(dialog)类型).当我导航到该对话框,然后尝试从该对话框目标导航到另一个片段时,对话框正在关闭,我认为这是可预测的行为.
Currently in my navgraph i have dialog fragment destination ( Fragment(dialog) type). When i'm navigating to this dialog and then try to navigate to another fragment from this dialog destination, dialog is closing which in my opinion is upredictable behaviour.
现在我只导航到下一个这样的片段.
Now i'm only navigating to the next fragment like this.
findNavController().popBackStack(R.id.testFragment, true)
我想不关闭对话框,而只导航到另一个对话框,例如默认片段.我该如何实现?
I want to dialog not closing and only navigate to another dialog like a default fragment. How can i achieve this ?
推荐答案
根据此问题,它按预期工作:
-
对话框是始终位于活动窗口上方的单独窗口.这意味着无论基础FragmentManager处于什么状态或您执行什么FragmentTransactions,对话框都将继续拦截系统后退按钮.
Dialogs are separate windows that always sit above your activity's window. This means that the dialog will continue to intercept the system back button no matter what state the underlying FragmentManager is in or what FragmentTransactions you do.
片段容器上的操作(即您的常规<fragment>
目标)不会影响对话框片段.如果在嵌套的FragmentManager上执行FragmentTransactions,则相同.
Operations on the fragment container (i.e., your normal <fragment>
destinations) don't affect dialog fragments. Same if you do FragmentTransactions on a nested FragmentManager.
Navigation的<dialog>
支持的初始版本没有考虑到这些限制,因此将对话目标与其他目标一样对待,因为从Navigation的角度来看,它们可以放回栈中并像其他任何东西一样对待. <fragment>
目的地.
The initial release of Navigation's <dialog>
support did not take these limitations into account and would treat dialog destinations just like any other in that, from Navigation's point of view, they could be put on the back stack and treated like any other <fragment>
destination.
实际上并非如此,我们对Navigation 2.1.0-alpha06进行了一些更改,以确保Navigation的世界状态与您在屏幕上实际看到的内容相匹配,并防止崩溃(如comment#5).
As that's not actually the case, we've made a few changes for Navigation 2.1.0-alpha06 to ensure that Navigation's state of the world matches what you actually see on the screen and prevent crashes like that in comment#5.
其摘要是,导航到非对话和非活动性目的地(例如目的地)时,现在会自动弹出<dialog>
目的地.作为参考,此操作已在 https://android-review.googlesource.com/996359 和 https://android-review.googlesource.com/1007662
因此,当您导航至非对话框目标时,所有对话框目标都将从后台堆栈中弹出.
So it is expected when you navigate to a non-dialog destination that any dialog destination is popped off the back stack.
这篇关于从DialogFragment导航到另一个片段时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!