Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin extensions for Android: How to use bundleOf

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.

like image 552
Malwinder Singh Avatar asked Mar 17 '18 23:03

Malwinder Singh


People also ask

How do you send data from one activity to another in 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. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I get bundle data on Android?

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.

What are the Kotlin Android extensions?

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.

Is bundleof() part of Android-Kotlin-extension?

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.

How do you extend a class in Kotlin?

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.

How to pass data between activities in Android Bundle using Kotlin?

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.


2 Answers

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
)
like image 82
Todd Avatar answered Oct 17 '22 11:10

Todd


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.

like image 12
Chandra Sekhar Avatar answered Oct 17 '22 12:10

Chandra Sekhar