Serializable and Parcelable)

Source: Internet
Author: User

 


In Android, how does one transmit objects in Intent? As far as I know, there are two methods: Bundle. putSerializable (Key, Object); Bundle. putParcelable (Key, Object); of course, these objects have certain conditions. The former implements the Serializable interface, and the latter implements the Parcelable interface, to make it easier for everyone to understand, I wrote a simple Demo as usual. Let's come with me step by step!

Step 1: Create an Android project named ObjectTranDemo (many classes !) The directory structure is as follows:

 

Step 2: Modify the main. xml layout file (here I have added two buttons). The Code is as follows:

To clipboardprint? <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Welcome to Mr wei's blog ."
/>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Serializable"
/>
<Button
Android: id = "@ + id/button2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Parcelable"
/>
</LinearLayout>

 

Step 3: create two classes: one is the Person. java Serializable interface, and the other Book. java implements the Parcelable interface. The Code is as follows:

Person. java:

 

To clipboardprint? Package com. tutor. objecttran;
Import java. io. Serializable;
Public class Person implements Serializable {
Private static final long serialVersionUID =-7060210544600464481L;
Private String name;
Private int age;
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;
}

}

 

Book. java:

 

To clipboardprint? Package com. tutor. objecttran;
Import android. OS. Parcel;
Import android. OS. Parcelable;
Public class Book implements Parcelable {
Private String bookName;
Private String author;
Private int publishTime;

Public String getBookName (){
Return bookName;
}
Public void setBookName (String bookName ){
This. bookName = bookName;
}
Public String getAuthor (){
Return author;
}
Public void setAuthor (String author ){
This. author = author;
}
Public int getPublishTime (){
Return publishTime;
}
Public void setPublishTime (int publishTime ){
This. publishTime = publishTime;
}

Public static final Parcelable. Creator <Book> CREATOR = new Creator <Book> (){
Public Book createFromParcel (Parcel source ){
Book mBook = new Book ();
MBook. bookName = source. readString ();
MBook. author = source. readString ();
MBook. publishTime = source. readInt ();
Return mBook;
}
Public Book [] newArray (int size ){
Return new Book [size];
}
};

Public int describeContents (){
Return 0;
}
Public void writeToParcel (Parcel parcel, int flags ){
Parcel. writeString (bookName );
Parcel. writeString (author );
Parcel. writeInt (publishTime );
}
}

 

Step 4: Modify ObjectTranDemo. java and create two new activities, ObjectTranDemo1.java and ObjectTranDemo2.java, to display the Person object data and Book object data respectively. The Code is as follows:

ObjectTranDemo. java:

 
To clipboardprint? Package com. tutor. objecttran;
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 ObjectTranDemo extends Activity implements OnClickListener {

Private Button sButton, pButton;
Public final static String SER_KEY = "com. tutor. objecttran. ser ";
Public final static String PAR_KEY = "com. tutor. objecttran. par ";
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SetupViews ();

}

// My consistent style
Public void setupViews (){
SButton = (Button) findViewById (R. id. button1 );
PButton = (Button) findViewById (R. id. button2 );
SButton. setOnClickListener (this );
PButton. setOnClickListener (this );
}
// Serializeable Method for passing objects
Public void SerializeMethod (){
Person mPerson = new Person ();
MPerson. setName ("frankie ");
MPerson. setAge (25 );
Intent mIntent = new Intent (this, ObjectTranDemo1.class );
Bundle mBundle = new Bundle ();
MBundle. putSerializable (SER_KEY, mPerson );
MIntent. putExtras (mBundle );

StartActivity (mIntent );
}
// Pacelable object transfer method
Public void PacelableMethod (){
Book mBook = new Book ();
MBook. setBookName ("Android Tutor ");
MBook. setAuthor ("Frankie ");
MBook. setPublishTime (2010 );
Intent mIntent = new Intent (this, ObjectTranDemo2.class );
Bundle mBundle = new Bundle ();
MBundle. putParcelable (PAR_KEY, mBook );
MIntent. putExtras (mBundle );

StartActivity (mIntent );
}
// Click the button to respond to the event
Public void onClick (View v ){
If (v = sButton ){
SerializeMethod ();
} Else {
PacelableMethod ();
}
}
}

 

ObjectTranDemo1.java:

 

To clipboardprint? Package com. tutor. objecttran;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class ObjectTranDemo1 extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

TextView mTextView = new TextView (this );
Person mPerson = (Person) getIntent (). getSerializableExtra (ObjectTranDemo. SER_KEY );
MTextView. setText ("You name is:" + mPerson. getName () + "/n" +
"You age is:" + mPerson. getAge ());

SetContentView (mTextView );
}
}

 

ObjectTranDemo2.java:

 

To clipboardprint? Package com. tutor. objecttran;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class ObjectTranDemo2 extends Activity {

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
TextView mTextView = new TextView (this );
Book mBook = (Book) getIntent (). getParcelableExtra (ObjectTranDemo. PAR_KEY );
MTextView. setText ("Book name is:" + mBook. getBookName () + "/n" +
"Author is:" + mBook. getAuthor () + "/n" +
"PublishTime is:" + mBook. getPublishTime ());
SetContentView (mTextView );
}
}

 

Step 5: Next, modify the AndroidManifest. xml file (declare the code of the two newly added activities, ObjectTranDemo1 and ObjectTranDemo2) as follows (lines 14th and 15 ):

 

To clipboardprint? <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. tutor. objecttran"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". ObjectTranDemo"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity android: name = ". ObjectTranDemo1"> </activity>
<Activity android: name = ". ObjectTranDemo2"> </activity>
</Application>
<Uses-sdk android: minSdkVersion = "7"/>
</Manifest>

 

Step 6: run the above project to view it:

Effect 1: First interface:

 

Result 2: Click Serializable.

 

Effect 3: Click Parcelable:

 

 

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.