What does bundle mean in Android?

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() andgetType() 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(it 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 =newIntent(ActivityContext,TargetActivity.class);Bundle dataMap =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{privatestaticfinallong serialVersionUID =1L;privateArrayList<FooObject> foos;publicFoo(ArrayList<FooObject> foos){this.foos = foos;}publicArrayList<FooObject> getFoos(){returnthis.foos;}}publicclassFooObjectimplementsSerializable{privatestaticfinallong serialVersionUID =1L;privateint id;publicFooObject(int id){this.id = id;}publicint getId(){return id;}publicvoid setId(int id){this.id = id;}}

Then:

Bundle dataMap =newBundle();ArrayList<FooObject> foos =newArrayList<FooObject>();
foos.add(newFooObject(1));
dataMap.putSerializable("key",newFoo(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:

@Overridepublicvoid 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:

@Overridepublicvoid 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).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s