Serializable 方式:
实例代码:
import java.io.Serializable;
/**
* Created by toby on 18-1-10.
*/
public class SendPersonUseSerializable implements Serializable {
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;
}
}
Parcelable 方式:
实例代码:
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by toby on 18-1-10.
*/
public class SendPersonUseParcelable implements Parcelable {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age;
public static final Creator<SendPersonUseParcelable> CREATOR = new Creator<SendPersonUseParcelable>() {
@Override
public SendPersonUseParcelable createFromParcel(Parcel in) {
SendPersonUseParcelable sendPersonUseParcelable = new SendPersonUseParcelable();
sendPersonUseParcelable.name = in.readString();
sendPersonUseParcelable.age = in.readInt();
return sendPersonUseParcelable;
}
@Override
public SendPersonUseParcelable[] newArray(int size) {
return new SendPersonUseParcelable[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeInt(age);
}
}
测试传递对象:
case 3:
SendPersonUseSerializable personUseSerializable = new SendPersonUseSerializable();
personUseSerializable.setName("Toby");
personUseSerializable.setAge(30);
intent = new Intent(this, ReceivePersonActivity.class);
intent.putExtra("person_serializable_data", personUseSerializable);
break;
case 4:
SendPersonUseParcelable personUseParcelable = new SendPersonUseParcelable();
personUseParcelable.setName("Tina");
personUseParcelable.setAge(28);
intent = new Intent(this, ReceivePersonActivity.class);
intent.putExtra("person_parcelable_data", personUseParcelable);
break;
测试接收对象:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.text.MessageFormat;
public class ReceivePersonActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_person);
TextView textView = findViewById(R.id.showPerson);
SendPersonUseSerializable personUseSerializable = (SendPersonUseSerializable)
getIntent().getSerializableExtra("person_serializable_data");
if (null != personUseSerializable) {
textView.setText(MessageFormat.format("Person name: {0}\nPerson age: {1}",
personUseSerializable.getName(), personUseSerializable.getAge()));
}
SendPersonUseParcelable personUseParcelable =
getIntent().getParcelableExtra("person_parcelable_data");
if (null != personUseParcelable) {
textView.setText(MessageFormat.format("Person name: {0}\nPerson age: {1}",
personUseParcelable.getName(), personUseParcelable.getAge()));
}
}
}
本文参考自 《Android 第一行代码》