In Android, there are two main ways for intent to pass objects: Bundle. putSerializable (Key, Object) and Bundle. putParcelable (Key, Object); of course, these objects have certain conditions. The former implements the Serializable interface, and the latter implements the Parcelable interface, the following is an example for you.
First, we create a project named ObjectTestDemo.
Then, modify the main. xml layout file and add two buttons.
View plaincopy to clipboardprint?
Copy codeThe Code is as follows: <? 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 Jesson'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>
<? 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 jesson'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>
[Code]
Next, we start to implement the project, and establish the Person. java to implement the Serializable interface, and the other Book. java to implement the Parcelable interface.
[Code]
Package com. test. 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;
}
}
Copy codeThe Code is as follows: package com. test. 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;
}
}
Copy codeThe Code is as follows: package com. test. 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 CREATOR = new Creator (){
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 );
}
}
Copy codeThe Code is as follows: package com. test. 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 CREATOR = new Creator (){
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 );
}
}
Modify ObjectTranDemo. java and create two new activities. One is ObjectTranDemo1.java and the other is ObjectTranDemo2.java. It is used to display the Person object data and Book object data respectively.
Code
Copy codeThe Code is as follows: package com. test. 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 ();
}
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 ();
}
}
}
Code
Copy codeThe Code is as follows: package com. test. 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 ();
}
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 ();
}
}
}
CodeCopy codeThe Code is as follows: package com. test. 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 () + "" +
"You age is:" + mPerson. getAge ());
SetContentView (mTextView );
}
CodeCopy codeThe Code is as follows: package com. test. 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 () + "" +
"You age is:" + mPerson. getAge ());
SetContentView (mTextView );
}
}
CodeCopy codeThe Code is as follows: package com. test. 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 () + "" +
"Author is:" + mBook. getAuthor () + "" +
"PublishTime is:" + mBook. getPublishTime (); setContentView (mTextView );
}
}
Copy codeThe Code is as follows: package com. test. 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 () + "" +
"Author is:" + mBook. getAuthor () + "" +
"PublishTime is:" + mBook. getPublishTime ());
SetContentView (mTextView );
}
}
The following is the most important link: Modify the AndroidManifest. xml file (add two newly added activities, ObjecttestDemo1 and ObjecttestDemo2) and declare the Code as follows (lines 14th and 15th ):
Code
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. test. 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 = ". ObjecttestDemo1"> </activity>
<Activity android: name = ". ObjecttestDemo2"> </activity>
</Application>
<Uses-sdk android: minSdkVersion = "7"/>
</Manifest>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. test. 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 = ". ObjecttestDemo1"> </activity>
<Activity android: name = ". ObjecttestDemo2"> </activity>
</Application>
<Uses-sdk android: minSdkVersion = "7"/>
</Manifest>