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 ());
}
}