0

I want to open a dialog fragment through a button in a fragment of the bottom navigation and make this button INVISIBLE when the dialog ends.

In order to make the button INVISIBLE, I think I need to work on the fragment that opened the dialog, not the dialog fragment, but I don't know how.

How do I know from the fragment that the dialog is ending

ybybyb
  • 1,385
  • 1
  • 12
  • 33
  • 1
    Does this answer your question? [DialogFragment and onDismiss](https://stackoverflow.com/questions/23786033/dialogfragment-and-ondismiss) – Amin Mousavi Jun 16 '21 at 20:29

1 Answers1

1

Try with the following code.

//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()
}
}
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7
  • Thanks for replying. Although I forgot and didn't mention it in the main text, I also use `Navigation Component` to launch `dialog fragment B` from `fragment A`. By using `nav_graph`. So I can't use the callback function to create a dialog like your code.. – ybybyb Jun 17 '21 at 18:11
  • I have update the code using navigation graph also – Dharmender Manral Jun 17 '21 at 18:41