如何在导航组件中从 Dialog 导航到 Fragment?

How to navigate from Dialog to Fragment in Navigation Component?

我试图在导航组件中从 DialogFragment 导航到 Fragment,但结果很奇怪。

当我从 DialogFragment 导航到 Fragment 时,背景片段正在更改为目标片段,当前对话框位于其顶部,而不是仅仅移动到目标片段。

这是导航图。

<navigation
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home"
    app:startDestination="@+id/titleScreen">

    <fragment
        android:id="@+id/titleScreen"
        android:name="com.example.android.navigationadvancedsample.homescreen.Title"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_title">
        <action
            android:id="@+id/action_title_to_about"
            app:destination="@id/aboutScreen"/>
    </fragment>
    <dialog
        android:id="@+id/aboutScreen"
        android:name="com.example.android.navigationadvancedsample.homescreen.About"
        android:label="@string/title_about"
        tools:layout="@layout/fragment_about">
        <action
            android:id="@+id/action_aboutScreen_to_register"
            app:destination="@id/register" />
    </dialog>
    <fragment
        android:id="@+id/register"
        android:name="com.example.android.navigationadvancedsample.formscreen.Register"
        android:label="fragment_leaderboard"
        tools:layout="@layout/fragment_leaderboard" />
</navigation>

为什么我会出现这种行为或如何解决?

我所说的修复是指正常的对话行为。比如说,我在片段 A 的顶部有一个对话框 D 并移动到片段 B 来自D 上的按钮,屏幕应显示 B。当我从 B 弹出时,它应该转到 A 之上的 D 的前一阶段.

你可以使用

view.getDialog().dismiss(); 

导航到 B 之后。但是这样一来,当您返回到 A 片段时,对话框将不可见。

如果你真的想让它可见,也许你应该尝试使用Fragment并把它假装成Dialog。就像在这些示例中 activity link.

感谢@musooff 提交 this bug

此问题已在 Navigation 2.1.0-alpha06 以及其他对话框不一致问题(例如 back button when Dialog is popped.

上得到修复

但是,如果可以,请更新到 2.1.0-beta02 或更高版本。