简体   繁体   English

如何从 DialogFragment 导航到 DialogFragment?

[英]How to navigate from DialogFragment to DialogFragment?

I have a fragment where I click a button and it opens a dialog(DialogFragment).我有一个片段,我单击一个按钮并打开一个对话框(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).在这个对话框中,我有一个按钮,如果我按下它,我想关闭这个对话框并打开另一个不同的对话框(DialogFragment)。 In this new dialog I want to do something similar, I want to make a button to go back to the previous dialog.在这个新对话框中,我想做类似的事情,我想将 go 按钮返回到上一个对话框。

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?如果我 go 从第二个对话框返回到第一个对话框以仍然显示参数,是否需要将参数传递给第二个对话框?

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.因此,当您从第一个或第二个对话框中按下关闭按钮时,后台不会出现不需要的对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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