12

While using Intent object we can put different types of data directly using its putExtra(). We can also put these extra data into a Bundle object and add it to Intent. So why do we need Bundle if we can do so using Intent directly?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Android Killer
  • 18,174
  • 13
  • 67
  • 90

4 Answers4

18

As you can see, the Intent internally stores it in a Bundle.

public Intent putExtra(String name, String value) {
    if (mExtras == null) {
        mExtras = new Bundle();
    }
    mExtras.putString(name, value);
    return this;
}
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Reno
  • 33,594
  • 11
  • 89
  • 102
9

Sometimes you need to pass only a few variables or values to some Other Activity, but what if you have a bunch of variable's or values that you need to pass to various Activities. In that case you can use Bundle and pass the Bundle to the required Activity with ease. Instead of passing single variable's every time.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • `mExtras = new Bundle();` isn't this convincing enough? If not go check the `Intent.java`'s source code yourself. – Reno Sep 25 '11 at 05:47
3

Let's assume you need to pass a Bundle from one Activity to another. That's why Intent allows you to add Bundles as extra fields.

EDIT: For example if you want to pass a row from a database along with some other data it's very convenient to put this row into a Bundle and add this Bundle to the Intent as a extra field.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • i am asking why ? we are going to put same extra data in bundle and add that bundle into intent object.that we can put directly into the intent object.Is not it ? – Android Killer Sep 24 '11 at 08:35
  • Not, it isn't. You can just have a `Bundle` you need to pass. Do you really want to copy it field-by-field to the `Intent`. And if keys of the `Bundle` and the `Intent` will overlap? You can't know all the things people do so just remember that you're able to put a `Bundle` into an `Intent` and when you need it you'll know what to do. – Michael Sep 24 '11 at 08:45
  • A little example: you want to pass a row from a database along with some other data. It's very convenient to store this row in a `Bundle`. In one of my applications I use a list of `Bundle`s to pass several rows from a database. – Michael Sep 24 '11 at 08:48
  • It's not a convenient reason buddy.See the below reason that's better. +1 for that anyway. – Android Killer Sep 24 '11 at 10:21
  • It depends on how you estimate a level of convenience. I gave you an example from the real life but I haven't ever faced a situation when I had to put a lot of variables to the `Bundle` just because there's a lot of variables. Usually people create `Parcelable` objects in such situations. It's much safer and easier to understand and to maintain. – Michael Sep 24 '11 at 10:57
1

I guess what @Lalit means is supposing your activity always passes the same variables to different intents, you can store all of them in a single Bundle in your class and simply use intent.putExtras(mBundle) whenever you need the same set of parameters.

That would make it easier to change the code if one of the parameters become obsolete in your code, for example. Like:

public class MyActivity {
    private Bundle mBundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mBundle = new Bundle();
        mBundle.putString("parameter1", value1);
        mBundle.putString("parameter2", value2);
    }
    private void openFirstActivity() {
        Intent intent = new Intent(this, FirstActivity.class);
        intent.putExtras(mBundle);
        startActivity(intent);
    }
    private void openSecondActivity() {
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtras(mBundle);
        startActivity(intent);
    }
}

OBS: As stated already, Intent stores the parameters in a internal Bundle, and it's worth noting that when you call putExtras, the internal Intent bundle doesn't point to the same object, but creates a copy of all variables instead, using a simple for like this:

for (int i=0; i<array.mSize; i++) {
    put(array.keyAt(i), array.valueAt(i));
}
Dielson Sales
  • 1,715
  • 1
  • 20
  • 25