Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 😀

>Solution :

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()

}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading