Clean Ways to Handle Android Bundles

Jaewoong Eum
The Startup

--

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.

Multiple usages

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.

Typecasting Serializable

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.

Default value

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! 💗

--

--

Jaewoong Eum
The Startup

Senior Android Developer Advocate @ Stream 🥑 • GDE for Android • OSS engineer. Love psychology, magic tricks, and writing poems. https://github.com/skydoves