androidkotlinandroid-dialogfragmentnavgraph

How to navigate from DialogFragment to DialogFragment?


I have a fragment where I click a button and it opens a dialog(DialogFragment). In this dialog, i have a button that if I press it, I want to dismiss this dialog and open another different dialog(DialogFragment). In this new dialog I want to do something similar, I want to make a button to go back to the previous dialog.

In my navigation graph I have this:

    <dialog android:id="@+id/dialog_first" android:name="com.example.dialog.FirstDialog" android:label="first" tools:layout="@layout/myfirstdialog">

    <argument android:name="phonenumber" app:argType="string"/>

    <action android:id="@+id/action_dialog_first_to_dialog_second" app:destination="@+id/dialog_second"/>
    
</dialog>


<dialog android:id="@+id/dialog_second" android:name="com.example.dialog.SecondDialog" android:label="second" tools:layout="@layout/myfirstdialog">

    <action android:id="@+id/action_dialog_second_to_dialog_first" app:destination="@+id/dialog_second"/>
    
</dialog>

In my first dialog, I recieve an argument to show it, if I travel to second dialog and come back to first dialog, I want to still have the argument in the dialog,is it needed to pass the argument to second dialog and pass it back then to the first dialog again to keep it or not?

Thats how Im doing it but when I click the button to navigate from the first dialog to the second dialog it only dismiss the dialog.

    btnGoToSecondDialog.setOnClickListener{
       dismiss()
       findNavController().navigate(
           FirstDialogDirections.action_dialog_first_to_dialog_second()
       )
    }

How is the best way to do this? How can I show the second dialog? Is it needed to pass the argument to the second dialog if I go back from the second dialog to the first to still showing the argument?


Solution

  • When you go from one dialog to another, if you delete the current dialog from the background and send the data in the current dialog to the second dialog, you will have done what you want.

    When you want back to first dialog you should delete the second dialog from the background and send the data to first dialog.

    Thus, when you press the dismiss button from the first or the second dialog, there will be no unwanted dialog in the background.