musooff
musooff

Reputation: 6852

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.

enter image description here

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.

Upvotes: 13

Views: 10492

Answers (2)

jpcv
jpcv

Reputation: 66

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.

Upvotes: 5

Klaudia
Klaudia

Reputation: 7

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.

Upvotes: 1

Related Questions