How to transmit object data in Android

Source: Internet
Author: User

In Android application development, if we need to transmit data between different modules (such as between different activities), there are usually two methods:

1. Use the intent object to carry data

By querying the API documentation of intent/bundle, we can know that,Intent/bundleData of the basic type and array data of the basic type can be transferred, andString/charsequenceTypes of data andString/charsequenceArray data. However, other types of data seem to be powerless. Otherwise, we can see that intent/bundle can be passed in the intent/bundle API.Parcelable(Package, package) andSerializable(Serialized) data and their array/List data.

Therefore, to transmit non-basic and non-string/charsequence data through intent/bundle, we need to implementParcelableInterface orSerializableInterface.

1.1 Use the parcelable interface to transmit data through intent/bundle

 /**  
* Represents a person's Information
* @ Author Gansc
*/
Public Class Personinfo Implements Parcelable
{
Public String INAME; // Person Name
Public Int Isex; // Gender
Public String IID; // ID card number
Public String imobilenumber; // Mobile phone number
Public String iemailaddr; // Email Address


//From parcelable
@ Override
Public IntDescribecontents ()
{
Return 0;
}
 //  From parcelable
// Save to package
@ Override
Public Void Writetoparcel (parcel DEST, Int Flags)
{
DeST. writestring (INAME );
DeST. writeint (isex );
DeST. writestring (IID );
DeST. writestring (imobilenumber );
DeST. writestring (iemailaddr );
}

// Among the types that implement the parcelable interface, there must be a static constant member field that implements the parcelable. Creator interface,
// And its name must be creator's
Public Static Final Parcelable. Creator < Personinfo > Creator
= New Parcelable. Creator < Personinfo > ()
{
// From parcelable. Creator
@ Override
Public Personinfo createfromparcel (parcel in)
{
Personinfo brief = New Personinfo ();

// Read data from packages
Brief. INAME = In. readstring ();
Brief. isex = In. readint ();
Brief. IID = In. readstring ();
Brief. imobilenumber = In. readstring ();

Brief. iemailaddr = In. readstring ();

Return brief;
}
   
// from parcelable. creator
@ override
Public personinfo [] newarray ( int size)
{< br> return New personinfo [size];
}< BR >};
}
 

2. Define a public static member variable, which is included in different modules and used to transmit data between different modules.

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.