Search code examples
androidkotlindialoglistener

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 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.


Solution

  • Create a callback parameter in your function, and call it in the 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
      }
    }