14

I am new to android application development, and can't understand what does bundle actually do for us.

Can anyone explain it for me?

nbro
  • 15,395
  • 32
  • 113
  • 196
Ejaz Karim
  • 3,676
  • 6
  • 36
  • 49

7 Answers7

23

I am new to android application development and can't understand what does bundle actually do for us. Can anyone explain it for me?

In my own words you can image it like a MAP that stores primitive datatypes and objects as couple key-value

Bundle is most often used for passing data through various Activities. Provides putType() and getType() methods for storing and retrieving data from it.

Also Bundle as parameter of onCreate() Activity's life-cycle method can be used when you want to save data when device orientation is changed (in this case activity is destroyed and created again with non null parameter as Bundle).

More about Bundle at its methods you can read reference at developer.android.com where you should start and then make some demo applications to get experience.

Demonstration examples of usage:

Passing primitive datatypes through Activities:

Intent i = new Intent(ActivityContext, TargetActivity.class);
Bundle dataMap = new Bundle();
dataMap.putString("key", "value");
dataMap.putInt("key", 1);
i.putExtras(dataMap);
startActivity(i);

Passing List of values through Activities:

Bundle dataMap = new Bundle();
ArrayList<String> s = new ArrayList<String>();
s.add("Hello");
dataMap.putStringArrayList("key", s); // also Integer and CharSequence
i.putExtras(dataMap);
startActivity(i);

Passing Serialized objects through Activities:

public class Foo implements Serializable {

   private static final long serialVersionUID = 1L;

   private ArrayList<FooObject> foos;

   public Foo(ArrayList<FooObject> foos) {
      this.foos = foos;
   }

   public ArrayList<FooObject> getFoos() {
      return this.foos;
   }        
}


public class FooObject implements Serializable {

   private static final long serialVersionUID = 1L;

   private int id;

   public FooObject(int id) {
      this.id = id;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }
}

Then:

Bundle dataMap = new Bundle();
ArrayList<FooObject> foos = new ArrayList<FooObject>();
foos.add(new FooObject(1));
dataMap.putSerializable("key", new Foo(foos));


Pass Parcelable objects through Activities:

There is much more code so here is article how to do it:


How to retrieve data in target Activity:

There is one magic method: getIntent() that returns Intent (if there are any data also with extended data) that started Activity from there method is called.

So:

Bundle dataFromIntent = getIntent().getExtras();
if (dataFromIntent != null) {
   String stringValue = dataFromIntent.getString("key");
   int intValue = dataFromIntent.getInt("key");
   Foo fooObject = (Foo) dataFromIntent.getSerializable("key");
   // getSerializble returns Serializable so we need to cast to appropriate object.
   ArrayList<String> stringArray = dataFromIntent.getStringArrayList("key");
}


Usage of Bundle as parameter of onCreate() method:

You are storing data in onSaveInstanceState() method as below:

@Override
public void onSaveInstanceState(Bundle map) {
   map.putString("key", "value");
   map.putInt("key", 1);
}

And restore them in onCreate() method (in this case is Bundle as parameter not null) as below:

@Override
public void onCreate(Bundle savedInstanceState) {
   if (savedInstanceState != null) {
      String stringValue = savedInstanceState.getString("key");
      int intValue = savedInstanceState.getString("key");
   }
   ...
}

Note: You can restore data also in onRestoreInstanceState() method but it's not common (its called after onStart() method and onCreate() is called before).

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
8

In general english: "It is a collection of things, or a quantity of material, tied or wrapped up together."

same way in Android "It is a collection of keys and its values, which are used to store some sort of data in it."

Harpreet
  • 2,990
  • 3
  • 38
  • 52
4

Bundle is generally used for passing data between various component. Bundle class which can be retrieved from the intent via the getExtras() method.

You can also add data directly to the Bundle via the overloaded putExtra() methods of the Intent objects. Extras are key/value pairs, the key is always of type String. As value you can use the primitive data types.

The receiving component can access this information via the getAction() and getData() methods on the Intent object. This Intent object can be retrieved via the getIntent() method. And the component which receives the intent can use the getIntent().getExtras() method call to get the extra data.

MainActivity

Intent intent = new Intent(MainActivity.this,SecondActivity.class);

Bundle bundle = new Bundle();

bundle.putString(“Key“, myValue);

intent.putExtras(bundle);

startActivity(intent);

SecondActivity

Bundle bundle = getIntent().getExtras();

String myValue= bundle.getString(“key“);
Stephen
  • 9,899
  • 16
  • 90
  • 137
santoshpatmca
  • 1,702
  • 2
  • 14
  • 9
3

A collection of things, or a quantity of material, tied or wrapped up together. it is the dictionary meaning...By the same Bundle is a collection of data. The data may be of any type i.e String, int,float , boolean and any serializable data. We can share& save the data of one Activity to another using the bundle Bundle.

Stephen
  • 9,899
  • 16
  • 90
  • 137
Pragnani
  • 20,075
  • 6
  • 49
  • 74
2

Consider it as a Bundle of data, used while passing data from one Activity to another.

The documentation defines it as

"A mapping from String values to various Parcelable types."

You can put data inside the Bundle and then pass this Bundle across several activities. This is handy because you don't need to pass individual data. You put all the data in the Bundle and then just pass the Bundle, instead of sending the data individually.

Swayam
  • 16,294
  • 14
  • 64
  • 102
1

It's literally a bundle of things; information: You put stuff in there (Strings, Integers, etc), and you pass them as a single parameter (the bundle) when use an intent for instance.

Then your target (activity) can get them out again and read the ID, mode, setting etc.

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • Thanks for you answer Nanne. So they are just like objects? we pass them while starting an activity and receive their values on another activity... true me if i am wrong. – Ejaz Karim Feb 18 '13 at 12:50
  • It is an object (it's a certain class) where you can put other objects. You have functions to add Integers, Strings etc with a name, and retrieve them again. – Nanne Feb 18 '13 at 12:54
  • Now I have clear idea thank you so much. – Ejaz Karim Feb 18 '13 at 12:55
1

A mapping from String values to various Parcelable types.Click here

Example:

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);

Send value from one activity to another activity.

MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22