Working effectively with lambda expressions
In this recipe, we are going to explore the concept of lambdas and closures. We are going to write part of an Android application code responsible for handling button-click actions.
Getting ready
In order to implement this recipe's code, you need to create a new Android application project using Android Studio IDE.
Let's assume we have the following class, which is a sort of a controller of the application view layer:
class RegistrationScreen : Activity() { private val submitButton: Button by lazy { findViewById(R.id.submit_button) } override fun onCreate(savedInstanceState: Bundle?) {
// hook function called once the screen is displayed } }
It contains a reference to the submitButton: Button
instance. Inside the onCreate()
function we are going to implement logic responsible for handling the button clicks. Once the button is clicked, we want to make it invisible.
In order to invoke some action when the button is clicked, we need to...