Parcel序列化
来源:互联网 发布:js的全局函数 编辑:程序博客网 时间:2024/06/16 06:43
下面的这段话摘自Parcelable的文档:
Interface for classes whose instances can be written to and restored from a Parcel
. Classes implementing the Parcelable interface must also have a static field called CREATOR
, which is an object implementing the Parcelable.Creator
interface.
从典型来看,实现该接口需要实现2个接口,writeToParcel(Parcel dest, int flags)和describeContents()。除此之外,还必须新建一个含有 Creator的属性。典型的用法如下:
public class User implements Parcelable{
private String name;
private String email;
private int age;
public User(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putString("name", getName());
bundle.putString("email", getEmail());
bundle.putInt("age", getAge());
dest.writeBundle(bundle);
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
Bundle bundle = source.readBundle();
return new User (bundle.getString("name"), bundle.getString("email"), bundle.getInt("age"));
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
如果碰到readBundle: bad magic number的错误,请检查是否多次使用source.readBundle()来读取同一个内容,第二次调用将会返回异常。
0 0
- Parcel序列化
- Android Parcel序列化实践
- Android序列化之Parcel
- 序列化机制之-Parcel
- Android 数据Parcel序列化过程源码分析
- Android中的序列化机制——Parcel与Parcelable
- 序列化手段(1)——parcel例子详解
- 安卓bundle传递序列化对象RuntimeException: Parcel android.os.Parcel@8325970: Unmarshalling unknown type code
- Parcel
- Parcel
- Parcel
- Android Intent 使用 Parcel 反序列化出错.
- Android Parcel
- Parcel用法
- Android Parcel
- 什么是parcel
- C++ Parcel
- android parcel
- Java冒泡排序
- 涉及多表查询的LINQ语句排错
- PHP版本VC6与VC9、Thread Safe与None-Thread Safe等的区别
- 5月2 JAG2014 Practice Contest A题 North North West
- 九大排序算法再总结
- Parcel序列化
- const 常见用法
- Android 图片平铺实现方式
- 非极大值抑制(Non-maximum suppression)在物体检测领域的应用
- [Leetcode 202, Easy] Happy Number
- 转:JAVA设计模式之单例模式
- Spring Boot实践应用开发(2)
- nc (NetCat) 网络安全工具介绍
- 学习Java基础记录