如何从对话框导航到导航组件中的片段?

时间:2019-05-31 02:35:14

标签: android android-fragments android-dialogfragment android-navigation android-architecture-navigation

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

enter image description here

当我从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 ,然后从 D 上的按钮移至片段 B strong>,屏幕应显示 B 。当我从 B 弹出时,它应该转到 A 上的 D 的上一阶段。

2 个答案:

答案 0 :(得分:2)

感谢@musooff提交了this bug

此问题已在导航2.1.0-alpha06和其他对话框不一致问题(如back button when Dialog is popped)中得到解决。

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

答案 1 :(得分:0)

您可以使用

view.getDialog().dismiss(); 

之后导航到B。但是,以这种方式,当您返回 A 片段时,对话框将不可见。

如果您真的希望它可见,也许您应该尝试使用Fragment并假装它为Dialog。就像在这些示例中使用活动link