Home > Net >  kotlin AlertDialog button doesn't run lambda or function stored in variable
kotlin AlertDialog button doesn't run lambda or function stored in variable

Time:06-07

I'm trying to create a new class that helps create AlertDialog messages. This class consists of a function that collects all parameters (title, content/text, buttons) and shows the popup automaically.

fun createPopupMessage(
    context: Context,         // get aplication context
    title: String? = null,    // get desired title as parameter
    content: String? = null,  // get content as a text
    button1: String? = null,  // define button1 text
    function1: Unit? = null,  // define button1 command (get some function)
    button2: String? = null,  // define button2 text
    function2: Unit? = null   // define button2 command (get some function)
) {
    val message = AlertDialog.Builder(context)
    if (title != null) message.setTitle(title)
    if (content != null) message.setMessage(content)
    if (button1 != null) message.setPositiveButton(button1) {_, _ -> function1} // ALERT: The expression "function1" is unused
    if (button2 != null) message.setNegativeButton(button2) {_, _ -> kotlin.run { function2 }} // ALERT: The expression "function2" is unused too even whith kotlin.run command.
    message.show()
}

The problem is that when I try to store a function in the function1 or function2 parameters, the function is not executed when the button is pressed, but when createPopupMessage() is created.

// running application
createPopupMessage(
            context = this,
            title = "Olá, mundo!",
            content = "Testando um novo comando ^-^",
            button1 = "Ok",
            function1 = myFunction() // myFunction is called and executed here :/
        )
// continue application

How can I store myFunction() in function1 variable and run it only when the button is pressed?

Note: I tryed to use function1 = { _ -> myFunction } as a lambda, but it doesn't work too. I tryed change the function1 type to ((Unit) -> Unit)? or ((Any) -> Unit)? but happens the same :/

Thanks for help :D

CodePudding user response:

If you need a lambda that takes no parameter, it should be defined as

function1: ()->Unit

and then you call it as:

function1()

so it should be like this:

fun createPopupMessage(
    context: Context,         // get aplication context
    title: String? = null,    // get desired title as parameter
    content: String? = null,  // get content as a text
    button1: String? = null,  // define button1 text
    function1: (()->Unit)? = null,  // define button1 command (get some function)
    button2: String? = null,  // define button2 text
    function2: (()->Unit)? = null   // define button2 command (get some function)
) {
    val message = AlertDialog.Builder(context)
    if (title != null) message.setTitle(title)
    if (content != null) message.setMessage(content)
    if (button1 != null) message.setPositiveButton(button1) { _, _ -> function1?() } 
    if (button2 != null) message.setNegativeButton(button2) { _, _ -> function2?() }
    message.show()

}

  • Related