在Android中经常会在两个Activity之间传输数据,又很多种方法可以实现,比如写在文件中,用SharedPreferences等等都可以实现,现在来介绍的是使用序列化的方式来传输数据。Android序列化一共有两种方式,一种是实现Serializable接口,另一种就是实现Parcelable接口。
实现Serializable接口 实现Serializable非常简单,只需要实现这个接口就可以了,没有实现Parcelable接口那么复杂。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 /** * 实现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 set Name(String name) { this.name = name; } public String getSex () { return sex; } public void set Sex(String sex) { this.sex = sex; } @Override public String toString () { return "Student{" + "name='" + name + '\' ' + ", sex=' " + sex + '\'' + '}'; } }
实现Parcelable接口 实现Parcelable接口比实现Serializable复杂了很多,但效率更高,是Android推荐的序列化方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 /** * 实现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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 /** * 实现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 set Name(String name) { this.name = name; } public String getSex () { return sex; } public void set Sex(String sex) { this.sex = sex; } @Override public String toString () { return "Student{" + "name='" + name + '\' ' + ", sex=' " + sex + '\'' + '}'; } }
Teacher 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 /** * 实现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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class MainActivity extends AppCompatActivity { private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set ContentView(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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class SecondActivity extends AppCompatActivity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set ContentView(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序列化已经介绍完了。
欢迎大家关注我的微信公众号,我会不定期的分享些Android开发的技巧