NiceToMytyuk
NiceToMytyuk

Reputation: 4307

How to handle buttons of custom Dialog using DialogFragment?

I have a custom dialog with X button, that dialog is called in a Fragment and here i have to manage all the clicks from the Alert.

Which is the best way to do so?

I actually was going to set the click listeners in the DialogFragment but i have to change some layout stuff and set variables from my Fragment so it will be better if i manage it from the fragment directly.

Here is my code now:

class ElencoDialog(private val testata: Testata, private val elimina: Boolean): DialogFragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btnInvia = view.findViewById(R.id.btnInvia)
        btnInvia.setOnClickListener {

        }
    }
}

And here is my fragment where i show the dialog:

class ElencoFragment : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = LettureListAdapter({
            ElencoDialog(it, false).show(parentFragmentManager, "ElencoDialog")
        }, {
            ElencoDialog(it, true).show(parentFragmentManager, "ElencoDialog")
        })
    }
}

So instead of managing the click from the DialogFragment how can i manage the clicks directly from my Fragment?

Upvotes: 0

Views: 691

Answers (2)

Henry Twist
Henry Twist

Reputation: 5990

First of all it is bad practice to have anything apart from the default constructor in a DialogFragment (or any Fragment). Although this might work initially, the system might need to recreate the fragment for various reasons, rotation, low memory etc. and it will attempt to use an empty constructor to do so. You should instead be using fragment arguments to pass simple data (covered in another question), a ViewModel for more complex data (I prefer this method anyway) or the new fragment results API, which I've outlined below.

But in answer to your specific question, to interact between your dialog fragment and main fragment, you have a few options:

Target fragment

You can set your original fragment as a target of your dialog but using setTargetFragment(Fragment). The fragment can then be retrieved safely from your dialog using getTargetFragment. It would probably be best practice to have your fragment implement an interface which you can cast to has the relevant callback methods.

Fragment results API

This is a relatively new API that attempts to replace the above, you can read more about it here: Communicating between fragments.

ViewModel

You can use a shared ViewModel scoped to the activity or parent fragment and keep your state in there. This would also solve the problem of having to pass your initial state through your fragment constructor. I won't explain how they work here as that's another question, but I would take a look here: ViewModel overview.

Upvotes: 1

Muhammad Ahmed
Muhammad Ahmed

Reputation: 1048

Pass callBack from fragment to your Dialog Fragment

class ElencoFragment() : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = LettureListAdapter({
            ElencoDialog(it, false){
//handle Callback
}.show(parentFragmentManager, "ElencoDialog")
        }, {
            ElencoDialog(it, true){
              //handle Callback
}.show(parentFragmentManager, "ElencoDialog")
        })
    }
}

Your Dialog Fragment

class ElencoDialog(private val testata: Testata, private val elimina: Boolean, block : () -> Unit): DialogFragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btnInvia = view.findViewById(R.id.btnInvia)
        btnInvia.setOnClickListener {
            block()
        }
    }
}

Upvotes: 0

Related Questions