Connection: http://blog.csdn.net/Android_Tutor/archive/2010/07/16/5740845.aspx
Intent can be used to transmit data between two acitifiers, such as int, string array, and list.
But what if I want to pass an object?
I found the information online and ran it.
Mian. xml:
<? 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 = "@ string/hello"
/>
<Button Android: Id = "@ + ID/btn_01"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: text = "serilizable"/>
<Button Android: Id = "@ + ID/btn_02"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: text = "parcelable"/>
</Linearlayout>
Mianactivity. Java
Package cn.edu. WTU;
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 intentdemo extends activity implements onclicklistener {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button btn_01 = (button) findviewbyid (R. Id. btn_01 );
Button btn_02 = (button) findviewbyid (R. Id. btn_02 );
Btn_01.setonclicklistener (this );
Btn_02.setonclicklistener (this );
}
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Switch (V. GETID ()){
Case R. Id. btn_01 :{
Intent intent = new intent (this, personview. Class );
Person mperson = new person ();
Mperson. setage (20 );
Mperson. setname ("moon ");
Bundle bundle = new bundle ();
Bundle. putserializable ("person", mperson );
Intent. putextras (bundle );
Startactivity (intent );
Break;
}
Case R. Id. btn_02 :{
Intent intent = new intent (this, bookview. Class );
Book = New Book ();
Book. setname ("manmonth ");
Book. settime ("1975 ");
Book. setauthor ("Brooks ");
Bundle bundle = new bundle ();
Bundle. putparcelable ("book", book );
Intent. putextras (bundle );
Startactivity (intent );
Break;
}
}
}
}
Book. Java:
Package cn.edu. WTU;
Import Android. OS. parcel;
Import Android. OS. parcelable;
Public class Book implements parcelable {
Private string name;
Private string author;
Private string time;
Public static final parcelable. creator <book> creator = new creator <book> (){
@ Override
Public book createfromparcel (parcel source ){
// Todo auto-generated method stub
Book mbook = New Book ();
Mbook. Name = source. readstring ();
Mbook. Time = source. readstring ();
Mbook. Author = source. readstring ();
Return mbook;
}
@ Override
Public book [] newarray (INT size ){
// Todo auto-generated method stub
Return New Book [size];
}
};
@ Override
Public int describecontents (){
// Todo auto-generated method stub
Return 0;
}
@ Override
Public void writetoparcel (parcel DEST, int flags ){
// Todo auto-generated method stub
DeST. writestring (name );
DeST. writestring (author );
DeST. writestring (time );
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public String gettime (){
Return time;
}
Public void settime (string time ){
This. Time = time;
}
Public String getauthor (){
Return author;
}
Public void setauthor (string author ){
This. Author = author;
}
}
Person. Java:
Package cn.edu. WTU;
Import java. Io. serializable;
Public class person implements serializable {
Private Static final long serialversionuid = 1l;
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;
}
}
Personview:
Package cn.edu. WTU;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;
Public class personview extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Textview text = new textview (this );
Person = (person) getintent (). getserializableextra ("person ");
Text. settext ("name:" + person. getname () + "/Nage:" + person. getage () + "/N ");
Setcontentview (text );
}
}
Bookview:
Package cn.edu. WTU;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;
Public class bookview extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Textview text = new textview (this );
Book = (book) getintent (). getparcelableextra ("book ");
Text. settext ("name:" + book. getname () + "/nautor:" + book. getauthor () + "/ntime:" + book. gettime ());
Setcontentview (text );
}
}