Sometimes back to look at their own andriod study, practice of the road, always found that some previously did not understand, now clear the reason, also will find some not much attention before, now see, very want to go deep understanding.
For example:Bundle.
In the life cycle of an activity, the first thing to do is the OnCreate method
@Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_modifyheadphoto);
}
By default, the red part above is the parameter of the OnCreate method, and the default execution method will be added automatically, and this part of the general situation, I do not pay attention to, you?
Today, let's figure out the role of this bundle and the difference between the intent and the.
One, Bundle:A mapping from String values to various parcelable types
A collection of key-value pairs
Class inheritance Relationships:
Java.lang.Object
Android.os.Bundle
The bundle class is a final class:
Public final class Bundle extends Objectimplements parcelable cloneable
Function: can be used as a communication between two activity.
Usage:
①, loading data:
Bundle Mbundle = new bundle ();
mbundle.putstring ("Datatag", "data to be passed");
Intent Intent = new Intent ();
Intent.setclass (Mainactivity.this, Destion.class);
Intent.putextras (Mbundle);
②, target activity parsing data
Bundle bundle = Getintent (). Getextras (); //Get bundles coming in
String data = bundle.getstring ("Datatag"); //read out data
Second, the meaning and role of intent is slightly ... Directly on the two comparisons:
Data is passed between two activity, and there are two ways to add data:
One is direct intent.putxx ();
The other is to BUNDLE.PUTXX () before calling public Intent Putextras (bundle extras) to add bundles.
In fact, the two are the same nature.
First look at the intent method:
Public Intent PutExtra (String name, Boolean value) {
if (Mextras = = null) {
Mextras = new Bundle ();
}
Mextras.putboolean (name, value);
return this;
}
Where Mextras is a private bundle variable defined internally by intent.
As can be seen, intent is actually called the corresponding put function of the bundle, that is, intent inside or with bundles to achieve data transfer, just encapsulated a layer.
The last method to invoke the value of the bundle is: Intent.putextras (bundle extras):
Public Intent Putextras (Bundle extras) {
if (Mextras = = null) {
Mextras = new Bundle ();
}
Mextras.putall (extras);
return this;
}
As you can see, the data in the previous bundle was added in batches to the bundle inside the intent.
In fact, and the above directly with the intent key value pair is the same principle.
In short, intent is designed to deliver data, bundles are designed to access data, and of course intent provides access to a subset of the data, but it is less professional and inflexible than bundles.
The difference between android-bundle cognition and intent