And Parcelable Serializable interface object serialization process is completed, and when we need Binder Intent need to use data transmitted via the two serialization. Also, we need to object persistence storage device or transmitted over the network to other clients, this use is also required to complete Serializale serialized object.
Serializable Interface
public class User1 implements Serializable{
private static final long serialVersionUID = 1L;
public int userId;
public String userName;
public String password;
@Override
public String toString() {
return "User1{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
private void startUser1(){
User1 user1 = new User1();
user1.userId = 1;
user1.userName = "name";
user1.password = "pwd";
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("user1",user1);
startActivity(intent);
}
private void getUser1(){
User1 user1 = (User1) getIntent().getSerializableExtra("user1");
Log.e("User1",user1.toString());
}
//结果
User1{userId=1, userName='name', password='pwd'}
Another serialized to a local
//序列化到本地
User1 user1=new User(1,"name","pwd");
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("user1.obj"));
out.writeObject(user1);
out.close;
//反序列化
ObjectInputStream in=new ObjectInputStream(new FileInputStream("user1.obj"));
User1 user1=(User1)in.readObject();
in.close();
Interface Parcelable
Parcelable interface is a dedicated Android SDK provides serialization and deserialization of the objects in a manner Android application Seriablizable compared to the better performance. Parcelable object that implements the interface can be achieved and can be serialized and transmitted through Intent Binder.
public class Book implements Parcelable{
public int num;
public Book(int num){
this.num = num;
}
protected Book(Parcel in) {
this.num = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(num);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book createFromParcel(Parcel in) {
return new Book(in);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public String toString() {
return "Book{" +
"num=" + num +
'}';
}
}
public class User2 implements Parcelable{
public int userId;
public String userName;
public String password;
public Book book;
public User2(int userId,String userName,String password,Book book){
this.userId = userId;
this.userName = userName;
this.password = password;
this.book = book;
}
protected User2(Parcel in) {
this.userId = in.readInt();
this.userName = in.readString();
this.password = in.readString();
this.book = in.readParcelable(Thread.currentThread().getContextClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.userId);
dest.writeString(this.userName);
dest.writeString(this.password);
dest.writeParcelable(this.book,0);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<User2> CREATOR = new Creator<User2>() {
@Override
public User2 createFromParcel(Parcel in) {
return new User2(in);
}
@Override
public User2[] newArray(int size) {
return new User2[size];
}
};
@Override
public String toString() {
return "User2{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", book=" + book +
'}';
}
}
private void startUser2() {
Book book = new Book(3);
User2 user2 = new User2(1,"name","pwd",book);
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("user2",user2);
startActivity(intent);
}
private void getUser2(){
User2 user2 = (User2) getIntent().getParcelableExtra("user2");
Log.e("User2",user2.toString());
}
//结果
User2{userId=1, userName='name', password='pwd', book=Book{num=3}}
note
- Static member variables belonging to the class does not belong to the object, it does not participate in the serialization process;
- Keyword tag with transient member variables do not participate in the serialization process;