How to navigate from Dialog to Fragment in Navigation Component?
H

2

13

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.

Hilton answered 31/5, 2019 at 2:35 Comment(7)
Can you file a bug against Navigation?Truncated
@Truncated is it really a bug? I just didn't want to file a bug if its sometime I am doing wrongHilton
What you're describing isn't normal dialog behavior - generally, selecting something from the dialog closes the dialog permanently (the user made their selection and there's nothing else for them to do in that dialog) - for that, you'd add an app:popUpTo="@+id/aboutScreen" app:popUpToInclusive="true". But you didn't ask for that behavior, you asked for the dialog to come back after you hit the back button, which is not something that is supported and should be filed as a feature request / bug.Truncated
@Truncated I am sure you know better, but the behavior that you are describing is applicable for AlertDialog in which I have simple basic actions like Yes Or No where user will choose and you don't need that dialog. But in case of DialogFragment, we generally have a bit complex dialogs. Suppose a SignUp Page where I show UserAgreement PopUp when user clicks on sign up button. And On my UserAgreement PopUp I would have links for PrivacyPolicy Page where user can go and should be able to come back to UserAgreement PopUpHilton
Every dialog should be managed by a DialogFragment (dialogs themselves don't survive configuration changes, etc), no matter how 'complex' it is. You're use case seems reasonable, which is why you should file a bug to make it possible.Truncated
Thank you for you feedback, I filed a bug.Hilton
DialogFragment does survive in the backstack when starting an activity from it and even supports onActivityResult(). I was using it with a text field and an option to populate the text field by selecting from a list in another screen. I'm trying to switch from an activity for each screen to a single activity with navigation component and ran into this issue for this one dialog. I would rather keep it a dialog because of how temporary it is and how it relates to the screen it shows over.Conte
T
5

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.

Trojan answered 25/7, 2019 at 10:37 Comment(2)
They have filed "Fixed", but I haven't able to test it. Thanks for notifyingHilton
You are welcome. Everything works correctly as far as I could test.Trojan
J
1

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.

Jemison answered 2/6, 2019 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.