1. Implement the parcelable Interface
For Android, the transmission of complex types is mainly to convert their classes into basic byte arrays, and data transmission between activities is implemented through intent. For binary data, the passed parameter is putextra (string
Name, parcelable value). For example, the bitmap class implements the parcelable interface. If you need to transmit images in the activity, you can directly use the bitmap type, of course, if it is drawable, You can first convert it to bitmap. You can refer to the article about the conversion methods between bitmap and drawable.
Related intent methods are also available.
Putextra (string name, parcelable [] value) // pass a parcelable Array
Putextra (string name, bundle value) // pass a bundle object
Putparcelablearraylistextra (string name, arraylist <? Extends parcelable> value) // transmits an array of objects that implement the parcelable interface.
Putstringarraylistextra (string name, arraylist <string> value) // transmits a string array.
Putintegerarraylistextra (string name, arraylist <integer> value) // transmits an integer array.
Here, we will remind you that intent also implements basic type arrays such as putextra (string name, int [] value) for integer packing types, and parcelable is more efficient than standard Java serialization, it is also used to exchange data in aidl. Therefore, the display serialization technology like parcelable can greatly improve the data exchange performance.
Ii. Use the global Singleton Mode
In Android, the activity transmission mainly relies on the intent class, so the efficiency of data transmission within the intent class still needs to be copied. If the data volume is large, you can also consider using the Singleton pattern Singleton mode to define a global static class. In this way, the transmission efficiency is the highest. You can refer to the Java design mode to understand its usage.
Iii. Implement the serializable Interface
If you have learned the traditional Java, You can implement the serializable interface through the serialization technology. In fact, this principle is similar to parcelable, and all of your classes are converted to basic types such as byte arrays. In Android, it is not very practical. I will not repeat it here. in intent, use the intent putextra (string name, serializable value) method.
Ii. Transmit data for ndefmessage
Send data: intent. putextra (tagservice. extra_save_msgs, MSGs );
Collect data:
If (intent. hasextra (extra_save_msgs )){
Parcelable [] msgs = intent. getparcelablearrayextra (extra_save_msgs );
Ndefmessage MSG = (ndefmessage) msgs [0];
}