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
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()
}
}