Android-bundle the difference between cognition and intent

Source: Internet
Author: User

Tag: Data ext Inherits Rri Create return section will also

Sometimes back to look at their own andriod study, practice of the road, always found some once unclear, now clear reason. You will also find some things that you haven't noticed before. Now I see, I want to know deeply.

For example:Bundle.

In the life cycle of an activity, the first thing to run is the OnCreate method

@Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_modifyheadphoto);
}

By default, the red section above is the parameter of the OnCreate method, and the default method of running it will be actively joined by itself, and this part of the general situation. I don't even care. What about 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: Capable of being used as a communication between two activity.

How to use:

①, 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 comparison:

Data is passed between two activity, and there are two ways to add data:
One is direct intent.putxx ();
Another is to BUNDLE.PUTXX () before calling public Intent Putextras (bundle extras) to join the bundle.

In fact the two are the same in 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;
}
Among them, Mextras is a private bundle variable defined internally by intent.
To see, intent is actually called the bundle corresponding to the put function, that is, intent internal or bundle to achieve data transfer, only a layer is encapsulated.

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;
}
Can see. In fact, the data in the previous bundle was added in batches to the bundle inside the intent.

In fact, it is the same principle as the above directly using the intent key value pair.

In short, intent is designed to deliver data, bundles are designed to access data, and of course intent provides access to part of the data, but it is unprofessional compared to bundles. Less flexible and more

Android-bundle the difference between cognition and intent

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.