Android-implemented Parcelable Object serialization instance

Source: Internet
Author: User
Tags object serialization

Bundle. putParcelable can be used to transmit objects, but the class of this object must implement the Parcelable interface.

The following is a simple example of passing objects between activities.

Parcel serializes parameters, and serializes class objects into a binary stream in full bits to improve transmission efficiency.

 

 


Parcelable class. The main class, that is, the class of the object to be transmitted, must implement the Parcelable interface.


[Java]
Import android. OS. Parcel;
Import android. OS. Parcelable;

Public class BenParcelable implements Parcelable {

Public String name;

Public int age;

Public String permission Sion;

Public BenParcelable (String name, int age, String partition sion ){
This. name = name;
This. age = age;
This. Permission sion = permission Sion;
}

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}

Public int getAge (){
Return age;
}

Public void setAge (int age ){
This. age = age;
}

Public String getsion Sion (){
Return Regression Sion;
}

Public void settransfer Sion (String transfer sion ){
This. Permission sion = permission Sion;
}

@ Override
Public int describeContents (){
Return 0;
}

@ Override
Public void writeToParcel (Parcel parcel, int flag ){
Parcel. writeString (name );
Parcel. writeInt (age );
Parcel. writeString (partition sion );
}

Public static final Creator <BenParcelable> CREATOR = new Creator <BenParcelable> (){
Public BenParcelable createFromParcel (Parcel in ){
Return new BenParcelable (in );
}

Public BenParcelable [] newArray (int size ){
Return new BenParcelable [size];
}
};

Private BenParcelable (Parcel in ){
Name = in. readString ();
Age = in. readInt ();
Sion = in. readString ();
}
}

Import android. OS. Parcel;
Import android. OS. Parcelable;
 
Public class BenParcelable implements Parcelable {
 
Public String name;
 
Public int age;
 
Public String permission Sion;
 
Public BenParcelable (String name, int age, String partition sion ){
This. name = name;
This. age = age;
This. Permission sion = permission Sion;
}
 
Public String getName (){
Return name;
}
 
Public void setName (String name ){
This. name = name;
}
 
Public int getAge (){
Return age;
}
 
Public void setAge (int age ){
This. age = age;
}
 
Public String getsion Sion (){
Return Regression Sion;
}
 
Public void settransfer Sion (String transfer sion ){
This. Permission sion = permission Sion;
}
 
@ Override
Public int describeContents (){
Return 0;
}
 
@ Override
Public void writeToParcel (Parcel parcel, int flag ){
Parcel. writeString (name );
Parcel. writeInt (age );
Parcel. writeString (partition sion );
}
 
Public static final Creator <BenParcelable> CREATOR = new Creator <BenParcelable> (){
Public BenParcelable createFromParcel (Parcel in ){
Return new BenParcelable (in );
}
 
Public BenParcelable [] newArray (int size ){
Return new BenParcelable [size];
}
};
 
Private BenParcelable (Parcel in ){
Name = in. readString ();
Age = in. readInt ();
Sion = in. readString ();
}
}
ParcelableActivity class: The Activity class that transmits objects.

 

[Java]
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class ParcelableActivity extends Activity {

Private Button myButton;

@ Override
Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

MyButton = (Button) findViewById (R. id. myButton );

MyButton. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View arg0 ){

BenParcelable benParcelable = new BenParcelable ("BenZeph", 23,
"Java/Android Engineer ");

Intent intent = new Intent ();

Intent. setClass (getApplicationContext (),
GetParcelableActivity. class );

Bundle bundle = new Bundle ();

Bundle. putParcelable ("Ben", benParcelable );

Intent. putExtras (bundle );

StartActivity (intent );

}
});
}
}

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
 
Public class ParcelableActivity extends Activity {
 
Private Button myButton;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
 
Super. onCreate (savedInstanceState );
 
SetContentView (R. layout. main );
 
MyButton = (Button) findViewById (R. id. myButton );
 
MyButton. setOnClickListener (new OnClickListener (){
 
@ Override
Public void onClick (View arg0 ){
 
BenParcelable benParcelable = new BenParcelable ("BenZeph", 23,
"Java/Android Engineer ");
 
Intent intent = new Intent ();
 
Intent. setClass (getApplicationContext (),
GetParcelableActivity. class );
 
Bundle bundle = new Bundle ();
 
Bundle. putParcelable ("Ben", benParcelable );
 
Intent. putExtras (bundle );
 
StartActivity (intent );
 
}
});
}
}
The GetParcelableActivity class receives the Activity class of the serialized object.

 

[Java]
<SPAN style = "FONT-SIZE: 18px"> <STRONG> import android. app. Activity;
Import android. OS. Bundle;

Public class GetParcelableActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

BenParcelable parcelable = getIntent (). getParcelableExtra ("Ben ");

System. out. println (parcelable. getName ());

System. out. println (parcelable. getAge ());

System. out. println (parcelable. getsion Sion ());

}

} </STRONG> </SPAN>

Import android. app. Activity;
Import android. OS. Bundle;
 
Public class GetParcelableActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
 
Super. onCreate (savedInstanceState );
 
SetContentView (R. layout. main );
 
BenParcelable parcelable = getIntent (). getParcelableExtra ("Ben ");
 
System. out. println (parcelable. getName ());
 
System. out. println (parcelable. getAge ());

System. out. println (parcelable. getsion Sion ());
 
}
 
}

 

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.