August 2017
Intermediate to advanced
440 pages
10h
English
The run and with functions are both accepts a lambda literal with a receiver as an argument and return its result. The difference between them is that run accepts a receiver, while the with function is not an extension function and it takes the object we are operating in as a parameter. Both functions can be used as an alternative to the apply function when we are setting up an object:
val button = findViewById(R.id.button) as Button
button.apply {
text = "Click me"
isVisible = true
setOnClickListener { /* ... */ }
}
button.run {
text = "Click me"
isVisible = true
setOnClickListener { /* ... */ }
}
with(button) {
text = "Click me"
isVisible = true
setOnClickListener { /* ... */ }
}
The difference between apply ...
Read now
Unlock full access