Kotlin extensions for Android: How to use bundleOf
Just to complete the other answers:
First, to use bundleOf
, need to add implementation 'androidx.core:core-ktx:1.0.0'
to the build.gradle
then:
var bundle = bundleOf("KEY_PRICE" to 50.0, "KEY_IS_FROZEN" to false)
How about this?
val bundle = bundleOf (
"KEY_PRICE" to 50.0,
"KEY_IS_FROZEN" to false
)
to
is a great way to create Pair
objects. The beauty of infix function with awesome readability.
If it takes a vararg
, you have to supply your arguments as parameters, not a lambda. Try this:
val bundle = bundleOf(
Pair("KEY_PRICE", 50.0),
Pair("KEY_IS_FROZEN", false)
)
Essentially, change the {
and }
brackets you have to (
and )
and add a comma between them.
Another approach would be to use Kotlin's to
function, which combines its left and right side into a Pair
. That makes the code even more succinct:
val bundle = bundleOf(
"KEY_PRICE" to 50.0,
"KEY_IS_FROZEN" to false
)