android - DialogFragment 从它导航到另一个 fragment 时关闭

标签 android android-fragments kotlin

目前在我的导航图中,我有对话 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:

  1. 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.

  2. 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/

相关文章:

android - 您如何从其源代码分发构建适用于 Android 的 FreeImage?

java - 异常 - 无法从资源中找到可绘制的图标

android - 只有在流程中添加帐户时,Google Signin 才会将显示名称返回为 null

android - 作为 ViewPager 的一部分更新 ListFragment 中的数据

android - onClickListener 在 fragment 中不起作用

kotlin - 如何等待所有协程完成?

android - FirebaseUser 类不下载/导入

Eclipse 无法识别我的 Android 设备

android - 尝试从 Kotlin 中的另一个 Activity 访问 EditText 时出现空指针异常

java - 如何为接口(interface)中的属性指定@Throws