Clean Ways to Handle Android Bundles

Jaewoong Eum
Nov 18, 2020 · 4 min read

The components of Android (Activity, Fragment, Service, BroadcastReceiver) are very independent of each other. It means they don’t have any dependencies on each other and it allows us to decide their entry points at any points. And we can decide and change their workflow by the desired anytime.

But the independent characteristics make it difficult to communicate with each other. So the Android SDK supports concepts of the Intent for holding an abstract description of an action to be performed, and the Bundle for passing data based on inter-process communication mechanism. They are one of the most important and basic things in Android development and we must use them for starting components or passing data between them. In this posting, we will look around how to handle them simply using the library Bundler.

Bundler

Bundler is an Android library that published recently to simplify creating instances of the Intent and inserting & retrieving extra values from Bundle.

Including in your project

Now let’s begin to include the Bundler in our project! We should add the below dependency to our build.gradle.

dependencies {
implementation "com.github.skydoves:bundler:1.0.2"
}

If you want to get the latest version, see here.

IntentOf

Many developers already use the bundleOf extension by thecore-ktx library. It is convenient to insert extra data into the Bundle as key-value pair type parameters and we can reduce the repeated putExtra codes.

But the Intent, we always have to use the scope functions to reduce repeated codes or just use the fully typed codes to construct. Because the Intent does not support a Builder. A similar approach to thebundleOf, the intentOf expression reduces the repeated codes and creates instances of Intent easily. Furthermore, it allows us to use various expressions to simplify the code that inserts values into the bundle and applying them to the Intent.

As we can see from the above codes, we can create an instance of the Intent with the Kotlin DSL style and put the extra value into the intent using the three different types of expressions. Also, it allows us to start an Activity in one scope like the below.

Bundle

When we want to get extra data from the previous component, we should use get…Extra functions by the intent. But we sometimes face inconveniences when we use them in projects.

We sometimes have to use the extra data in the multiple local functions, and it makes us consider how to design for getting the extra data whenever we need it. Here are some candidates we mostly think of.

  • Just get the data from the intent every time.
  • A getter function.
  • var or lateinit var property.
  • val property.

Let’s see from the first candidate. Getting data from the Intent every time when we need it is that we should write the same codes multiple times. If we want to change something related to it in the future, we must find and fix all of the distributed same codes. Also, we should get the data from the map every time. That doesn’t look like the best choice. Instead, writing a getter function to get the data from the Intent would be a better choice. But writing a new function just for getting extra data looks like a little overkill. Then how about the var or lateinit var property? Whatever we choose, the var property can be changed at any time by mistake. Also if we try to use before initializing the lateinit var property, we will get a not initialized exception.

For those reasons, the Bundler adopted the last candidate, val property, and provides to initialize it much easier. We should get the value easily, and the value must be immutable. The bundle can make it possible. It is an expression that initializes extra values from the Intent lazily. So the values will be initialized when we access them.

We can also use the bundle expression for initializing a field in our local functions.

If we want to get a Serializable type from the intent, we have to do typecasting. But by using the bundle expression, we can initialize it and do typecasting it simply.

Some extra functions don’t support the default value likegetParcelableExtra, getSerializableExtra, getStringArrayListExtra, etc. Sometimes we need to initialize using a default value when Bundle can’t find the matched value from the given key. The bundle expression supports initializing with a default value if there is no matched value from the Bundle map.

Fragment

We can set the arguments for Fragments using the intentOf like the below codes.

We can also use the bundle expression the same as in the Activity.

Conclusion

We looked around how to handle the Intent and Bundle more simply using the Bundler library. If you pursue cleaner ways to write codes, it’s worth a try. And you can also see the usage of the Bundler in the demo project. Thanks for reading! 💗

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Jaewoong Eum

Written by

A software engineer: Android, IoT. Interested in Psychology, Magic Tricks, and writing poems. https://github.com/skydoves

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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