I am new to android application development, and can't understand what does bundle actually do for us.
Can anyone explain it for me?
An Android App Bundle is a publishing format that includes all your app's compiled code and resources, and defers APK generation and signing to Google Play.
A Bundle is very much like a Java Map object that maps String keys to values. It's used to pass information between activities and other application components. It's also used by the framework to capture and restore state information.
As you can see in examples, bundle can operate with objects, but Intents cannot. For this reason, Usage of the bundle is better and flexible. In brief, Bundle aims to store data, while Intent aims to transfer value.
Here's how we get the object passed by a Bundle: Intent intent=getIntent(); // Instantiate a Bundle Bundle bundle=intent. getExtras(); //Get data inside Persion Persion persion= (Persion) bundle. getSerializable("persion"); text_show.
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.
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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With