如何在 kotlin 的 Android 中覆盖对话框片段中的 onCancel?

How to override onCancel in Dialog Fragment in Android in kotlin?

我想显示一个进度条DialogFragment。 它会一直显示,直到它被取消或关闭。 如果用户按下后退按钮或触摸对话框外部,它可以被取消,如果用户在任务完成之前没有取消它,它就会被取消。 所以,我想为两者都设置 listeners 以便我可以根据情况进行响应。 正在从 Fragment.

调用该对话框

根据 this,我无法设置 listeners,而是必须 override 方法。 我的主要问题是,我不知道如何在 kotlin 中做到这一点。 我在下面写了一些代码,但它不完整。请在需要的地方更正代码。

我现在只尝试实施 onCancel。请告知是否需要以某种不同的方式实施 onDismiss。 按照解决方案 here, 这就是我对 Fragment:

进行编码的方式
class MyFragment: Fragment(), DialogInterface.OnCancelListener {

    // other code

    private fun myFun() {
        // show progress dialog
        val myDialog = DialogProgress()
        myDialog.show(childFragmentManager, "null")

        // todo the long task of downloading something
        // myDialog.dismiss()
    }

    override fun onCancel(dialog: DialogInterface?) {
        // User canceled the dialog
        Toast.makeText(activity, "Process canceled by user!", Toast.LENGTH_SHORT).show()
        // todo
    }

}

这是我的 DialogFragment 代码:

class DialogProgress: DialogFragment() {

    override fun onCancel(dialog: DialogInterface?) {
        super.onCancel(dialog)
        // need help here
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)

        // show progress dialog
        val v = activity!!.layoutInflater.inflate(R.layout.dialog_progress, null)
        v.findViewById<TextView>(R.id.progress_message).text = "Fetching data"

        return activity!!.let {
            val builder = AlertDialog.Builder(it, R.style.ThemeOverlay_AppCompat_Dialog)
            builder
                .setView(progressView)
                .create()
        }

    }
}

上面的代码我需要帮助的地方,我不知道如何将下面的Java代码从上面给出的解决方案link转换成kotlin

@Override
public void onDismiss(final DialogInterface dialog) {
    super.onDismiss(dialog);
    Fragment parentFragment = getParentFragment();
    if (parentFragment instanceof DialogInterface.OnDismissListener) {
        ((DialogInterface.OnDismissListener) parentFragment).onDismiss(dialog);
    } 
}

请注意,这是给 onDismiss 的,我想要给 onCancel

Java 代码可以简单地转换为 kotlin 代码,方法是将其粘贴到 Android Studio 中,并且应该会出现 pop-up。 这是java代码的转换:

override fun onCancel(dialog: DialogInterface?) {
    super.onCancel(dialog)
    val parentFragment = parentFragment
    if (parentFragment is DialogInterface.OnCancelListener) {
        (parentFragment as DialogInterface.OnCancelListener).onCancel(dialog)
    }
}