Skip to content Skip to sidebar Skip to footer

What Does Bundle Mean In Android?

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

Solution 1:

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:

Intenti=newIntent(ActivityContext, TargetActivity.class);
BundledataMap=newBundle();
dataMap.putString("key", "value");
dataMap.putInt("key", 1);
i.putExtras(dataMap);
startActivity(i);

Passing List of values through Activities:

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

Passing Serialized objects through Activities:

publicclassFooimplementsSerializable {

   privatestaticfinallongserialVersionUID=1L;

   private ArrayList<FooObject> foos;

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

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


publicclassFooObjectimplementsSerializable {

   privatestaticfinallongserialVersionUID=1L;

   privateint id;

   publicFooObject(int id) {
      this.id = id;
   }

   publicintgetId() {
      return id;
   }

   publicvoidsetId(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:

BundledataFromIntent= getIntent().getExtras();
if (dataFromIntent != null) {
   StringstringValue= dataFromIntent.getString("key");
   intintValue= dataFromIntent.getInt("key");
   FoofooObject= (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:

@OverridepublicvoidonSaveInstanceState(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:

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
   if (savedInstanceState != null) {
      StringstringValue= savedInstanceState.getString("key");
      intintValue= 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).

Solution 2:

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."

Solution 3:

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

Intentintent=newIntent(MainActivity.this,SecondActivity.class);

Bundlebundle=newBundle();

bundle.putString(“Key“, myValue);

intent.putExtras(bundle);

startActivity(intent);

SecondActivity

Bundle bundle = getIntent().getExtras();

String myValue= bundle.getString(“key“);

Solution 4:

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.

Solution 5:

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.

Post a Comment for "What Does Bundle Mean In Android?"