简体   繁体   English

从调用者 class 捕获对话框关闭事件

[英]Catch dialog close event from caller class

I have a custom dialog.我有一个自定义对话框。 How can I catch dialog close event from class which call dialog?如何从调用对话框的 class 捕获对话框关闭事件?

class DialogShow {
    companion object {
    fun showDialog() {
      //some logic
      val customDialog = CustomDialog(account, context)
      customDialog.onDismissListener = {
           // here I need to return close event
      }
      customDialog.show(activity,"rawer")
    }
    

 }
}

calss CallDialog{
  DialogShow.showDialog()
  //here I need to catch dialog dismiss
}

I need to catch this event from kotlin and Java classes.我需要从 kotlin 和 Java 类中捕获此事件。

Create a callback parameter in your function, and call it in the onDismissListener:在你的function中创建一个回调参数,在onDismissListener中调用:

class DialogShow {
  companion object {
    fun showDialog(context: Context, onDismiss: ()->Unit) {
      //some logic
      val customDialog = CustomDialog(account, context)
      customDialog.onDismissListener = {
           onDismiss()
      }
      customDialog.show(context, "rawer")
    }
  }
}

fun foo() {
  DialogShow.showDialog(context) {
    // code that is run after dialog is closed
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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