Android. How to correctly create DialogFragment?

25 Views Asked by At

I have custom DialogFragment, for example MyErrorDialogFragment, which can retrieve some data via arguments like: title, description:

   fun newInstance(
            title: String? = "",
            description: String? = "",
        ): MyErrorDialogFragment = MyErrorDialogFragment().apply {
            arguments = bundleOf(
                TITLE to title,
                DESCRIPTION to description,
            )
        }

And my dialog has only one button, and the button click is processed via fragment result api:

 with(findViewById<Button>(R.id.btn_complete)) {
                    setOnClickListener {
                        dialog.dismiss()
                        setFragmentResult(
                         key,
                         bundleOf(),
                     )
                  }
               }

Now I need to create a wrapper over this fragment (also DialogFragment), which can pass data to the MyErrorDialogFragment (via newInstance), get the result from the MyErrorDialogFragment via fragment result api and perform its own handling, can this be implemented?

I do not quite understand how to correctly create a MyErrorDialogFragment inside my wrapper and how to process the result.

I create a wrapper so that I can connect a view model to it, since in my MyErrorDialogFragment I can't do it, it is a basic dialog where often the view model is not needed

Please help me.

0

There are 0 best solutions below