Differences between Android-Bundle cognition and Intent

Source: Internet
Author: User

Differences between Android-Bundle cognition and Intent

From time to time, let's look back at our Andriod's learning and practice path. We always find that some of the things we didn't understand before are clear, and now we know why. We also find some things that we didn't pay much attention to before, I really want to have a deep understanding of it.

For example, Bundle.

In the lifecycle of an Activity, the onCreate method must be executed first.

 @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_modifyheadphoto);
}

By default, the above red part is the parameter of the onCreate method. The default method will be automatically added. In general, I will not pay attention to this part. What about you?

Today, let's figure out the role of this Bundle and its difference with Intent.

I. Bundle: A mapping from String values to various Parcelable types

Set of key-value pairs

Class inheritance relationship:

Java. lang. Object
Android. OS. Bundle

The Bundle class is a final class:
Public final class Bundle extends Objectimplements Parcelable Cloneable

Role: it can be used for communication between two activities.

Usage:

① Load data:

Bundle mBundle = new Bundle ();

MBundle. putString ("DataTag", "data to be uploaded ");

Intent intent = new Intent ();
Intent. setClass (MainActivity. this, Destion. class );
Intent. putExtras (mBundle );

② Target Activity parsing data

Bundle bundle = getIntent (). getExtras (); // get the passed bundle

String data = bundle. getString ("DataTag"); // read data

2. The meaning and function of Intent are omitted... Directly compare the two:

Data is transmitted between two activities. There are two ways to attach data:
One is direct intent. putxx ();
The other is to bundle. putxx () first, and then call public Intent putExtras (Bundle extras) to add bundle.

In fact, the two are essentially the same.

First look at the Intent method:

Public Intent putExtra (String name, boolean value ){
If (mExtras = null ){
Mexico tras = new Bundle ();
}
Mexico tras. putBoolean (name, value );
Return this;
}
Here, mExtras is a private Bundle variable defined inside intent.
We can see that intent actually calls the corresponding put function of bundle. That is to say, intent still uses bundle to implement data transmission, but encapsulates a layer.

The method used to pass the value through Bundle is Intent. putExtras (Bundle extras ):

Public Intent putExtras (Bundle extras ){
If (mExtras = null ){
Mexico tras = new Bundle ();
}
Mexico tras. putAll (extras );
Return this;
}
We can see that the data in the previous bundle is added to the bundle in intent in batches.

In fact, it is the same principle as directly using Intent to transmit key-value pairs.

In short, Intent is designed to transmit data, bundle is designed to access data, and of course intent also provides access to some data, but it is not professional and flexible than bundle.

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.