Transmit complex types such as ndefmessage between activities

Source: Internet
Author: User

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];

}

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.