What does bundle mean in Android?

android, bundle

Solution

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:

- Parcel data to pass between Activities using Parcelable classes

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

Problem

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

Original source

Related problems