Bundle in Android: What It Is and How It Works
In this article
A Bundle in Android is a container that maps string keys to values of various types, used to pass data between components such as activities, fragments, and services, and to save and restore state. It is not a word in the slang sense; it is a standard technical term in the Android SDK. The class is android.os.Bundle, and it implements Parcelable so it can be sent across process boundaries. In practice, you use a Bundle whenever you need to hand a small set of named values from one part of an app to another, or when the system needs to persist UI state across configuration changes or process death. The rest of this article explains what a Bundle is, how it works under the hood, where it is used, common pitfalls, and how it compares to alternatives.
What a Bundle Actually Is
A Bundle is a key-value store built on top of Android's Parcel mechanism. Keys are strings, and values can be primitives (int, long, float, boolean, double), strings, arrays of those types, Parcelable objects, Serializable objects, and a few other supported types such as ArrayList of certain types. It is not a general-purpose database or a persistent store; it is designed for small amounts of data passed in memory or serialized into a Parcel for inter-process communication (IPC).
The class lives in the Android framework and has been part of the platform since the early API levels. It is standard, not informal or slang. You will see it in official documentation, code samples, and API references. The term comes from the general English word "bundle" meaning a collection of things tied together, which matches its role as a grouped set of values.
How a Bundle Works Internally
Internally, a Bundle holds an ArrayMap of keys to values. When you call methods like putString, putInt, or putParcelable, the Bundle stores the value in that map. When the Bundle needs to cross a process boundary or be saved, it is written to a Parcel using its writeToParcel method. The Parcel is a fast, in-memory serialization format that Android uses for IPC. This is why values in a Bundle must be either primitives, Parcelable, or Serializable; the system needs to know how to write and read them.
Because the Bundle is Parcelable, it can be attached to an Intent via putExtras and retrieved with getExtras. It can also be passed to a Fragment via setArguments, and it is used by the activity lifecycle to save instance state in onSaveInstanceState. The same class is used in Activity, Service, BroadcastReceiver, and ContentProvider interactions.
Where Bundles Are Used in Android
Bundles appear in several standard places. The most common are:
- Activity intents: You attach a Bundle to an
IntentwithputExtrasand read it in the target activity withgetIntent().getExtras(). - Fragment arguments: You create a Bundle, put values in it, and call
setArguments(bundle)before attaching the fragment. This survives configuration changes and process death better than setting fields directly. - Saved instance state: In
onSaveInstanceState(Bundle outState), you put values that should be restored inonCreateoronRestoreInstanceState. - Services and broadcasts: You can pass a Bundle as extras in an
Intentused to start a service or send a broadcast. - Navigation component: The Android Navigation component uses Bundles to pass arguments between destinations, often generated from safe-args classes.
In each case, the Bundle is a short-lived transport for a small set of values. It is not meant to carry large objects, bitmaps, or long lists; those should be stored elsewhere (a database, file, or ViewModel) and referenced by ID or key.
How to Use a Bundle: A Worked Example
Suppose you have a MainActivity that starts a DetailActivity and needs to pass a user ID and a display name. The standard pattern is:
- Create a
Bundleobject. - Put the values with typed methods such as
putInt("userId", 42)andputString("displayName", "Alex"). - Create an
Intentfor the target activity and callputExtras(bundle). - Call
startActivity(intent). - In the target activity, call
getIntent().getExtras()and read values withgetInt("userId")andgetString("displayName").
For fragments, the pattern is similar but you call setArguments(bundle) before the fragment is added, and read with getArguments(). Always use the same key strings in both places; a typo returns the default value (0, null, false) rather than throwing an error, which is a common source of bugs.
Common Problems and Limitations
Bundles are convenient but have real limits. The most important are:
- Size limit for IPC: Passing a Bundle through an
Intentto another process is subject to the Binder transaction buffer limit, which is roughly 1 MB per transaction on most devices. Exceeding it throws aTransactionTooLargeException. This is why you should not put large bitmaps or long lists in a Bundle. - Type safety: Keys are strings and values are retrieved with casts or typed getters. A wrong key or wrong type returns a default or throws a
ClassCastException. Use constants for keys and keep put/get types matched. - Process death: Saved instance state Bundles are not persisted forever. The system may drop them under memory pressure, so critical data should be saved to disk or a database.
- Not for large data: Use a ViewModel, database, or file for large or complex data, and pass only an ID or key in the Bundle.
A common misconception is that a Bundle is a persistent store. It is not; it is an in-memory transport and state holder. Another misconception is that any object can go in a Bundle. Only supported types can, and custom objects must implement Parcelable or Serializable.
Bundle vs. Other Options
Compared with other ways to pass data, a Bundle is the standard for small, typed values across Android components. A ViewModel is better for sharing data within a screen or across configuration changes without serialization. A database or file is better for large or persistent data. SharedPreferences is for small persistent key-value settings, not for passing data between components. Using a Bundle is usually the simplest correct choice when the data is small and the destination is an activity, fragment, or service.
Frequently Asked Questions
Is a Bundle the same as an Intent extra?
Not exactly. An Intent can carry extras directly via putExtra methods, and it can also carry a Bundle via putExtras. A Bundle is the underlying container; an Intent extra is one way to attach it. Both end up as key-value data in the Intent.
Can I put any object in a Bundle?
No. Values must be primitives, strings, arrays of supported types, or objects that implement Parcelable or Serializable. Other objects cannot be written to a Parcel and will fail at runtime.
Why do I get a TransactionTooLargeException?
You are passing too much data through a Binder transaction, usually a large Bundle in an Intent. Reduce the size, pass an ID instead, or store the data elsewhere and read it from the target component.
Should I use a Bundle or a ViewModel?
Use a Bundle for small values passed between components or saved in instance state. Use a ViewModel for data shared within a screen that should survive configuration changes without serialization. They solve different problems and are often used together.
Are Bundle keys case-sensitive?
Yes. Keys are strings and comparisons are case-sensitive, so "userId" and "userid" are different keys. Use constants to avoid mismatches.
Does a Bundle survive app restart?
No. A Bundle in an Intent or fragment argument is not persisted across a full app restart. Saved instance state may be restored after process death, but it is not a durable store. For persistence, use a database, file, or SharedPreferences.