在Android中经常会在两个Activity之间传输数据,又很多种方法可以实现,比如写在文件中,用SharedPreferences等等都可以实现,现在来介绍的是使用序列化的方式来传输数据。Android序列化一共有两种方式,一种是实现Serializable接口,另一种就是实现Parcelable接口。
实现Serializable接口
实现Serializable非常简单,只需要实现这个接口就可以了,没有实现Parcelable接口那么复杂。
/**
* 实现Serializable接口
*/
public class Student implements Serializable {
private String name;
private String sex;
public Student() {
}
public Student(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
实现Parcelable接口
实现Parcelable接口比实现Serializable复杂了很多,但效率更高,是Android推荐的序列化方式。
/**
* 实现Parcelable
*/
public class Teacher implements Parcelable {
private String name;
private String sex;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.sex);
}
public Teacher() {
}
public Teacher(String name, String sex) {
this.name = name;
this.sex = sex;
}
protected Teacher(Parcel in) {
this.name = in.readString();
this.sex = in.readString();
}
public static final Parcelable.Creator<Teacher> CREATOR = new Parcelable.Creator<Teacher>() {
@Override
public Teacher createFromParcel(Parcel source) {
return new Teacher(source);
}
@Override
public Teacher[] newArray(int size) {
return new Teacher[size];
}
};
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
看到实现Parcelable接口是不是瞬间就懵逼了,用AndroidStudio的的可以安装一个插件,可以自动的将实现Parcelable接口。来张效果图大家感受下。(插件的名字叫Android parcelable code generator)
具体应用
上面我们已经实现了这两个接口,那么怎么来使用用这个呢?接下来就来用个demo来演示下,Student实现
Serializable接口,Teacher实现Parcelable接口,将这两个类从MainActivity传输给SecondActivity并在SecondActivity中显示出来。
Student.java
/**
* 实现Serializable接口
*/
public class Student implements Serializable {
private String name;
private String sex;
public Student() {
}
public Student(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
Teacher
/**
* 实现Parcelable
*/
public class Teacher implements Parcelable {
private String name;
private String sex;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.sex);
}
public Teacher() {
}
public Teacher(String name, String sex) {
this.name = name;
this.sex = sex;
}
protected Teacher(Parcel in) {
this.name = in.readString();
this.sex = in.readString();
}
public static final Parcelable.Creator<Teacher> CREATOR = new Parcelable.Creator<Teacher>() {
@Override
public Teacher createFromParcel(Parcel source) {
return new Teacher(source);
}
@Override
public Teacher[] newArray(int size) {
return new Teacher[size];
}
};
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
Teacher teacher = new Teacher("teacher", "man");
bundle.putParcelable("teacher", teacher);
Student student = new Student("student", "femal");
bundle.putSerializable("student", student);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mTextView = (TextView) findViewById(R.id.textview);
Intent intent = getIntent();
mTextView.append(intent.getExtras().getSerializable("student").toString() + "\n");
mTextView.append(intent.getExtras().getParcelable("teacher").toString());
}
}
运行结果
到这里Android序列化已经介绍完了。
本博客同步发表在http://hjxandhmr.github.io/2016/06/19/Android-SerializeMethod/