Difference between Intent and Bundle in Android

  • 2021-11-02 02:19:57
  • OfStack

Preface

Bundle translates into Chinese, which means "bundling". It is often used to pass parameters between Activity. It is not very popular at the beginning of the previous 1, because Intent can pass itself. Intent. putExtra ("key", value), why use Bundle?

It happened that my little friend asked Android about the difference between Intent and Bundle, which is hereby summarized as follows:

Difference between Intent and Bundle in Value Transfer

First of all, from the use:

Intent Mode:

Suppose you need to pass data from the page A to B and then to C.

On the A page:


 Intent intent=new Intent(MainActivity.this,BActivity.class);
 intent.putExtra("String","MainActivity Value in ");
 intent.putExtra("int",11);
 startActivity(intent);

On the B page:

You need to receive data in the B page first


 Intent intent = getIntent();
 string = intent.getStringExtra("String");
 key = intent.getIntExtra("int",0);

Then send the data to the C page


 Intent intent=new Intent(BActivity.this,CActivity.class);
 intent.putExtra("String1",string);
 intent.putExtra("int1",key);
 intent.putExtra("boolean",true);
 startActivity(intent);

It can be seen that the inconvenient place to use is that one piece of data needs to be taken out on the B page and then one piece is transmitted to the C page.

If Bundle is used, the transmitted Bundle object can be directly taken out on the B page and then transmitted to the C page.

Bundle Mode:

On the A page:


 Intent intent = new Intent(MainActivity.this, BActivity.class);
 Bundle bundle = new Bundle();
 bundle.putString("String","MainActivity Value in ");
 bundle.putInt("int",11);
 intent.putExtra("bundle",bundle);
 startActivity(intent);

Receive data on the B page:


Intent intent = getIntent();
bundle=intent.getBundleExtra("bundle");

Then send the data in the B page:


 Intent intent=new Intent(BActivity.this,CActivity.class);
 // Can be passed to CActivity Extra value 
 bundle.putBoolean("boolean",true);
 intent.putExtra("bundle1",bundle);
 startActivity(intent);

Summary:

Bundle can operate on objects, while Intent cannot. Compared with Intent, Bundle has more interfaces and is more flexible to use, but Intent is still needed to complete data transfer when using Bundle. In a word, Bundle aims to store data, while Intent aims to transfer values.

Then look at the source code of put method of intent:


 public @NonNull Intent putExtra(String name, Parcelable value) {
  if (mExtras == null) {
   mExtras = new Bundle();
  }
  mExtras.putParcelable(name, value);
  return this;
 }

It can be seen that the data transmitted by Bundle is also used internally.

Digression

Why isn't Bundle directly replaced by Hashmap?

Bundle is internally implemented by ArrayMap, The internal implementation of ArrayMap is two arrays, one int array is to store the corresponding subscript of object data, one object array stores key and value, and the internal use of 2-point method to sort key, so when adding, deleting and searching data, it will use 2-point method to find, which is only suitable for small data operation. If the data volume is relatively large, its performance will deteriorate. However, HashMap has an array + linked list structure, so when the amount of data is small, Entry Array of HashMap occupies more memory than ArrayMap. Because most of the scenarios using Bundle are small data, I have never seen a scenario where more than 10 data are transferred between two Activity, so in contrast, using ArrayMap to save data in this case has advantages in operation speed and memory occupation, so using Bundle to transfer data can ensure faster speed and less memory occupation.

Another reason is that if Intent is used to carry data in Android, it needs to be of basic type or serializable type, HashMap is serialized by Serializable, and Bundle is serialized by Parcelable. In the Android platform, it is recommended to use Parcelable to realize serialization. Although it is complicated to write, it has less overhead. Therefore, in order to serialize and deserialize data more quickly, the system encapsulates Bundle class, which is convenient for us to transmit data.


Related articles: