Android Bundle
Android Bundle
A Bundle is a key value data structure in Android used for passing data between components such as activities, fragments, and services. It serializes data into a format the operating system can transmit across process boundaries and preserve across configuration changes. Understanding how bundles work and their limitations is important for managing data flow and state persistence in Android applications. By the end of this lesson, you will be able to:
- Explain what a
Bundleis and how it stores data. - Describe common use cases for bundles across Android components.
- Identify the size limitations of bundles and their implications.
- Use bundles correctly for instance state preservation.
How a Bundle Works
A Bundle stores data as key value pairs where the keys are strings and the values can be primitives, strings, Parcelable objects, Serializable objects, or nested bundles. Internally, it uses an ArrayMap for storage and supports serialization through the Parcel mechanism, which is how Android transmits data across process boundaries via Binder IPC.
val bundle = Bundle().apply {
putString("user_name", "skydoves")
putInt("user_age", 30)
putBoolean("is_premium", true)
}
val name = bundle.getString("user_name")
val age = bundle.getInt("user_age")
Each put method stores a value under a string key, and the corresponding get method retrieves it. The type must match at both ends; calling getInt on a key that holds a string returns the default value rather than throwing an exception.
Bundles also support storing arrays, lists of Parcelable, and nested Bundle objects. This flexibility makes them suitable for a wide range of data shapes, but the lack of compile time type checking means key name typos and type mismatches are only caught at runtime. Using constants for key names and creating extension functions for reading specific types helps prevent these errors.
Supported Types
The full set of types that a Bundle can store includes: Boolean, Byte, Char, Short, Int, Long, Float, Double, String, CharSequence, Parcelable, Serializable, Bundle, and arrays or ArrayList variants of most of these. Each type has a dedicated put and get method pair.
Passing Data Between Activities
When starting an activity with an Intent, you attach data through putExtra(), which stores the values in the intent's internal Bundle:
val intent = Intent(this, DetailActivity::class.java).apply {
putExtra("article_id", 42)
putExtra("article_title", "Kotlin Coroutines")
}
startActivity(intent)
// In DetailActivity
val id = intent.getIntExtra("article_id", -1)
val title = intent.getStringExtra("article_title")
The extras bundle travels with the intent through the system and is available in the target activity's onCreate() method. For type safety, consider using Kotlin extension properties or Navigation component's Safe Args plugin instead of raw string keys, which avoids mismatched key names and type casting errors.
This interview continues for subscribers
Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.
Become a Sponsor