Gary Vaughan Jr
Gary Vaughan Jr

Reputation: 91

How can I turn this into a lambda?

Android studio is giving me a squiggly saying that I should/could turn this into a lambda. I'm just getting back into my android.

popup.setOnMenuItemClickListener(object : PopupMenu.OnMenuItemClickListener {
    override fun onMenuItemClick(item: MenuItem): Boolean {
        if (item.itemId === R.id.action_vitals) {
            val vitalsIntent = Intent(this@DashboardActivity, VitalsActivity::class.java)
            vitalsIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
            startActivity(vitalsIntent)
        }
        if (item.itemId === R.id.action_devices) {
            val devicesIntent = Intent(this@DashboardActivity, DevicesActivity::class.java)
            devicesIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
            startActivity(devicesIntent)
        }
        return false
    }
})

This is the squiggly "object : PopupMenu.OnMenuItemClickListener"

Upvotes: 0

Views: 691

Answers (2)

jordan_terry
jordan_terry

Reputation: 468

There are a couple of comments and answers suggesting to use alt + enter in IntelliJ/Android Studio and that will handle converting your code into a Lambda for you. However, it might be worth covering why it is suggesting that to you.

A lambda is a function that isn't declared e.g. fun someFunction() but instead is immediately passed as a parameter to another function. This lambda will then be executed by some other code elsewhere in the app. Essentially a Lambda is a shorthand function e.g:

val lambda: () -> Unit = {
    // Some code could go inside this Lambda here    
}

An important concept for you here is a SAM (Single Abstract Method) type. This simply refers to an interface that defines a single abstract function that needs to be implemented. In your example PopupMenu.OnMenuItemClickListener is a Java interface that has a single abstract method void onMenuItemClick(MenuItem item). SAM types can be written in shorthand with the body of the lambda becoming the body of abstract function.

You've correctly written this as an anonymous object which is fine; but it could be written more concisely with a lambda which Android studio is suggesting. Another helpful tidbit, in Kotlin if a function or lambda is the sole or final parameter in a list of parameters it can be moved outside of the braces of the function or they can be removed entirely.

So your code will have been converted to something like this:

    enterpopup.setOnMenuItemClickListener {
        if (item.itemId === R.id.action_vitals) {
                val vitalsIntent = Intent(this@DashboardActivity, VitalsActivity::class.java)
                vitalsIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                startActivity(vitalsIntent)
            }
            if (item.itemId === R.id.action_devices) {
                val devicesIntent = Intent(this@DashboardActivity, DevicesActivity::class.java)
                devicesIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                startActivity(devicesIntent)
            }
            return false
    }

Upvotes: 2

charles-allen
charles-allen

Reputation: 4081

You can use Alt+Enter to show a list of "suggestions" (called inspections in JetBrains language). Selecting the suggestion will perform the conversion automatically for you.

IntelliJ 2019.2 brings an improvement to this feature (which should come to Android​ Studio soon), showing more info about the top suggestion and adding Alt+Shift+Enter as a shortcut to directly apply it (bypassing the list) https://www.jetbrains.com/idea/whatsnew/#v2019-2

enter image description here

Upvotes: 1

Related Questions