目前在我的导航图中,我有对话 fragment 目的地( fragment (对话)类型)。当我导航到此对话框然后尝试从此对话框目标导航到另一个 fragment 时,对话框正在关闭,我认为这是可预测的行为。
现在我只导航到这样的下一个 fragment 。findNavController().popBackStack(R.id.testFragment, true)
我想对话框不关闭,只导航到另一个对话框,如默认 fragment 。我怎样才能做到这一点?
最佳答案
根据 this issue ,这是按预期工作的:
There's a couple of problems here relating to how Dialogs and Fragments work:
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.
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.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.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.
The summary of this is that
<dialog>
destinations are now automatically popped when navigate to a non-dialog and non-activity destination, such as a destination. For reference, this was done in https://android-review.googlesource.com/996359 and https://android-review.googlesource.com/1007662
因此,当您导航到非对话目标时,预计任何对话目标都会从后台堆栈中弹出。
关于android - DialogFragment 从它导航到另一个 fragment 时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58607960/