片段如何知道对话框已关闭?

How can the fragment know that the dialog is closed?

我想在 bottom navigationfragment 中通过 button 打开一个 dialog fragment,并在对话框结束时制作此按钮 INVISIBLE

为了制作按钮 INVISIBLE,我想我需要处理打开对话框的片段,而不是对话框片段,但我不知道怎么做。

我如何从片段中知道对话正在结束

尝试使用以下代码。

//Receiver class
class CustomResultReceiver(handler: Handler?, receiver: 
ResultReceiverCallBack) :
ResultReceiver(handler), Serializable {
private var mReceiver: ResultReceiverCallBack = receiver

override fun onReceiveResult(resultCode: Int, resultData: Bundle) 
{
mReceiver?.onDialogCancel(true, resultCode, resultData)
}

interface ResultReceiverCallBack {
fun onDialogCancel(action: Boolean, resultCode: Int, resultData: 
 Bundle)
}

}


//Fragment A
Class 
FragmentA:Fragment(),CustomResultReceiver.ResultReceiverCallBack 
{
private lateinit var binding: FragmentABinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentABinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: 
 Bundle?) {
 super.onViewCreated(view, savedInstanceState)

 binding.dialogButton.setOnClickListener{
 findNavController().navigate(
 R.id.action_FragmentA_to_FragmentB,
 Bundle().apply { putSerializable("key", 
 CustomResultReceiver(Handler(requireContext().mainLooper), 
  this@FragmentA))})
 }
 }

 override fun onDialogCancel(action: Boolean, resultCode: Int, 
 resultData: 
 Bundle) {
 toast(“Dialog cancel“)
}
} 


//Fragment B as Dialog fragment
Class FragmentB:DialogFragment(){
private lateinit var binding: FragmentBBinding
private var resultReceiver: ResultReceiver? = null

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentBBinding.inflate(inflater, container, false)
return binding.root
}

 override fun onViewCreated(view: View, savedInstanceState: 
 Bundle?) {
 super.onViewCreated(view, savedInstanceState)
  arguments.let {
  resultReceiver = arguments?.getSerializable("key") as 
  CustomResultReceiver
  }

 }
override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
resultReceiver.onDialogCancel()
}
}