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.