Documentation says:
fun bundleOf(vararg pairs: Pair<String, Any?>): Bundle
Returns a new Bundle with the given key/value pairs as elements.
I tried:
val bundle = bundleOf {
Pair("KEY_PRICE", 50.0)
Pair("KEY_IS_FROZEN", false)
}
But it is showing error.
This example demonstrates how to pass data between activities using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Here's how we get the object passed by a Bundle: Intent intent=getIntent(); // Instantiate a Bundle Bundle bundle=intent. getExtras(); //Get data inside Persion Persion persion= (Persion) bundle. getSerializable("persion"); text_show.
Kotlin Android Extensions: What’s this? Kotlin Android Extensions are another Kotlin plugin that is included in the regular one, and that will allow recovering views from Activities, Fragments, and Views in an amazing seamless way.
I have added the plugin apply plugin: 'kotlin-android-extensions' , but bundleOf () doesn't resolve itself. Is there something else to set up? bundleOf has nothing to do with android-kotlin-extension. It is part of core library. Ideally it should be available. @toobsco42 bundleOf is part of the Android KTX extension functions.
Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done via special declarations called extensions. For example, you can write new functions for a class from a third-party library that you can't modify.
Android bundle to pass data between activities using Kotlin. This example demonstrates how to pass data between activities using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
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
)
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With