简体   繁体   中英

how to handle Dialog Clicks in fragment

How does one handle button clicks in a Dialog fragment. I would like to run method in fragment once a dialog button is clicked, the example given in dev docs works for activities but not fragments.

MyAlertDialog

class MyAlertDialog : DialogFragment() {
    
    var listener: MyListener

    interface MyListener {
        onPositiveClick(dialog: DialogFragment)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        try {
            listener = context as MyListener
        } catch (e: ClassCastException) {

            throw ClassCastException((context.toString() + " must implement MyDailogListener")
        }
    }

    override fun onCreateDialog(savedInstanceState:Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage("Do you want to reset the score.")
                .setPositiveButton("Confirm",
                    DialogInterface.OnClickListener { dialog, id ->
                        ...
                    })
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

MyFragment

class MyFragment : Fragment(), MyAlertDialog.MyListener {

    ...

    fun launchAlertDialog() {
        val dailog = MyAlertDialog().also {
            it.show(requireActivity().supportFragmentManager, "DialogInfoFragment")
        } 
    }

    override fun onDialogPostiveCLick(dialog: DialogFragment) {
        Log.i(TAG, "Listener returns a postive click")
    }
}

The DialogFragment is launched from the activity not from the fragment despite being called in the fragment. Linking the interface this way will only work if you are connecting to an activity.

If you are only updating data, you could pass the ViewModel into the DialogFragment through the constructor.

Fragment

fun launchAlertDialog() {
    val fragment = MyFragment(viewModel)
    fragment.show(requireActivity().supportFragmentManager, "MyAlertDialog")
}

DialogFragment

class MyAlertDialog(val viewModel: MainViewModel) : DialogFragment() {
    overrideOnCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage(...)
                .setPositiveButton("My Text", DialogInterface.OnClickListener { dialog, id -> 
                    viewModel.updateMyData()
                })
            builder.create()
        } 
    } ?: throw IllegalStateException("Activity cannot be null")

}

You could also use this approach here to run a function in the Fragment. https://stackoverflow.com/a/65116644/3943340

Keep in mind that setTargetFragment is now deprecated, as of API 28(could be wrong on the API).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM