简体   繁体   中英

How to navigate from Dialog to Fragment in Navigation Component?

I am trying to navigation from DialogFragment to Fragment in Navigation Component, but getting weird result.

在此输入图像描述

When I navigate from DialogFragment to Fragment , background fragment is changing to target fragment with current dialog on top of it, instead of just moving to target fragment.

Here is the navigation graph.

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

Why I am getting this behavior or how to fix it?

By fixing I mean normal dialog behavior. Say, I have a dialog D on top of a fragment A and move to a fragment B from a button on D , the screen should show B . And when I pop back from B , it should go to previous stage of D on top of A .

Thanks @musooff for filing this bug

This problem was fixed on Navigation 2.1.0-alpha06, along with others dialog inconsistencies like back button when Dialog is popped .

However, update to 2.1.0-beta02 or higher if you can.

You could use

view.getDialog().dismiss(); 

after navigate to B. But in that way, dialog won't be visible when you came back to A fragment.

If you really want it to be visible, maybe you should try to use Fragment and pretend it to be Dialog. Like in these example with activity link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM