12

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.

Community
  • 1
  • 1
musooff
  • 6,412
  • 3
  • 36
  • 65
  • Can you [file a bug against Navigation](https://issuetracker.google.com/issues/new?component=409828)? – ianhanniballake May 31 '19 at 02:43
  • @ianhanniballake is it really a bug? I just didn't want to file a bug if its sometime I am doing wrong – musooff May 31 '19 at 02:49
  • 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. – ianhanniballake May 31 '19 at 04:41
  • @ianhanniballake 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 PopUp – musooff May 31 '19 at 05:18
  • 1
    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. – ianhanniballake May 31 '19 at 05:44
  • Thank you for you feedback, I filed a bug. – musooff May 31 '19 at 06:19
  • 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. – Pilot_51 Nov 21 '19 at 21:49

2 Answers2

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.

jpcv
  • 66
  • 6
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.

Klaudia
  • 7
  • 4