You won’t believe this one weird trick to handle Android Intent extras with Kotlin

  • It’s now possible to get/set the extra directly on the Intent instance, instead of passing it as a parameter.
  • Naming the companion object has made the usage from Java much more readable, although the signature of the getter/setter is untouched.
  • There’s now a slight difference between handling the getter/setter inside the Activity and outside, and that’s due to one essential property of extension functions: scope. These extensions are scoped to where they are declared, which in this case is the companion object of the Activity. Because companion objects are special, they “extend their scope” to the class they’re declared in, so SampleActivity can access them directly. If you want to access a scope from outside, you need to force this to become something else, and you do that through special lambdas that have a receiver. Notice how we did it in the gist by using the with method from the standard library: it takes an object, and a lambda inside of which this is the object we passed (and so its scope is accessible). Note that the run and apply functions would work as well, I just find them less readable in this case.
  • You can now read and write the extra as if it was a field of the Intent.
  • The getter/setter logic is now encapsulated in a single declaration.
  • The Java signature is untouched.
  • Changed the delegate signature slightly to match the documentation.
  • Used the by keyword to bind the delegate.
  • Dropped the Delegate suffix to make it more readable.
  • The companion object extends the abstract class ActivityCompanion, and IntentOptions is split into a different object.
  • You can now easily create the Intent for the Activity from outside, and even “configure it”, thanks to a lambda that has the IntentOptions object as the receiver, inheriting the scope with all its extensions, and is given the Intent instance as its parameter (named it, unless you give it a name inside the lambda)!
  • Because the companion object doesn’t contain the IntentOptions anymore, the new extension Intent.options has been provided to make it easy to access the options inside the Activity; it uses the same trick as above to access the scope. The Java signature also changed slightly.




Android GDE & Lead Android Engineer @Clue

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrating Gesture Controls into your Applications — Kai SDK

Accelerate — Continuous Delivery

Revisiting Java Applications Performance & Scalability with RDBMS

Xero’s journey towards world-class engineering

7 Tips to Work on Before Outsourcing Software Development

Use case for Python Tuple

Space Shooter Challenge: Smart Enemy Type

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eugenio Marletti

Eugenio Marletti

Android GDE & Lead Android Engineer @Clue

More from Medium

How to Handle States in Jetpack Compose

Tried making bubbles

Handle Rotation with ExoPlayer in Jetpack Compose

Reduce APK Size