Preface
Bundle is translated into Chinese and means "bundle". It is often used to pass parameters between activities. It was not very popular at the beginning. The reason is that Intent itself can be passed, ("key", value), why do you still use Bundle?
It happened that my friend asked about the difference between Android's value transfer Intent and Bundle, so I will summarize it here:
The difference between Intent and Bundle in passing values
First of all, from the use:
Intent method:
Suppose you need to pass data from page A to B and then to C.
In page A:
Intent intent=new Intent(,); ("String","Value in MainActivity"); ("int",11); startActivity(intent);
In page B:
You need to receive data in page B first
Intent intent = getIntent(); string = ("String"); key = ("int",0);
Then send the data to page C
Intent intent=new Intent(,); ("String1",string); ("int1",key); ("boolean",true); startActivity(intent);
It can be seen that the inconvenient thing when using it is that you need to take out the data one by one on page B and then transfer it to page C one by one.
If you use Bundle, you can directly take out the transmitted Bundle object on page B and then transfer it to page C.
Bundle method:
In page A:
Intent intent = new Intent(, ); Bundle bundle = new Bundle(); ("String","Value in MainActivity"); ("int",11); ("bundle",bundle); startActivity(intent);
Receive data on page B:
Intent intent = getIntent(); bundle=("bundle");
Then send the data in page B:
Intent intent=new Intent(,); //Can be passed extra value to CActivity ("boolean",true); ("bundle1",bundle); startActivity(intent);
Summarize:
Bundle can operate on objects, but Intent cannot. Bundle has more interfaces than Intent, and is more flexible to use, but using Bundle still requires the help of Intent to complete data transfer. In short, Bundle is designed to store data, while Intent is designed to pass values.
Then look at the source code of the intent put method:
public @NonNull Intent putExtra(String name, Parcelable value) { if (mExtras == null) { mExtras = new Bundle(); } (name, value); return this; }
You can see that the data transmitted by Bundle is actually used internally.
Off topic
Why doesn't Bundle use Hashmap directly instead?
The internal implementation of Bundle is implemented by ArrayMap. The internal implementation of ArrayMap is two arrays, an int array stores the corresponding subscripts of the object data, and an object array stores the key and value. The key is sorted internally using dichotomy. Therefore, when adding, deleting, and searching data, dichotomy will be used to search, which is only suitable for small data volume operations. If the data volume is relatively large, its performance will degrade. HashMap has an array + linked list structure, so when the data volume is small, the Entry Array of HashMap takes up more memory than ArrayMap. Because most scenarios using Bundle are small data volumes, I have never seen scenarios where more than 10 data are passed between two activities, so in contrast, using ArrayMap to save data in this case has advantages in both operation speed and memory footprint. Therefore, using Bundle to pass data can ensure faster speed and less memory footprint.
Another reason is that if you use Intent to carry data in Android, the data needs to be a basic type or a serializable type. HashMap uses Serializable for serialization, while Bundle uses Parcelable for serialization. In the Android platform, it is recommended to use Parcelable to achieve serialization. Although the writing method is complicated, the overhead is smaller. Therefore, in order to serialize and deserialize data more quickly, the system encapsulates the Bundle class to facilitate our data transmission.
OK, the above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.