This is a four part series of how to setup and user Lambda Expressions in Android. In Part 2, we’ll use Lambda expressions with RxJava.
If you are tired of writing boilerplate code, then you should try out Java 8. Let’s write some code.
Enabling Lambda Feature on Gradle
We need to edit your
We can now write shorter code. In this example we’ll implement
on a button to demonstrate this. The old way of doing this looks like this:Now with Lambdas.
Lambda expressions contain double colons :: . These refer to a new syntax in Java 8 known as
. You can reference a class or instance and pass along the method that will handle the event:
It’s that simple. Source code is available on GitHub.
Known issues with using the Jack toolchain
- Instant Run does not currently work with Jack and will be disabled while using the new toolchain.
- There’s no support for Data Binding
- Annotation support for libraries such as Dagger 2 to show you in your Android project may not work with Jack. The most recent experimental releases are starting to add this support.
The work-around for this is using Retrolambda instead.
That’s it for now
In Part 2, we’ll use Lambda expressions with RxJava. Happy coding.