Parcelable接口介绍与使用
来源:互联网 发布:linux mv 多个文件夹 编辑:程序博客网 时间:2024/06/06 07:28
1. Parcelable接口
Parcelable的实例可以被写入和恢复一个 Parcel
类。一个类实现Parcelable接口也必须有一个公开的静态的常态的字段CREATOR,这是实现 Parcelable.Creator
的对象。
实现Parcelable案例:
public class Student implements Parcelable {private String name;// 你要序列化的数据private int age;// 你要序列化的数据public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}// get和set方法,略// ...<span style="color:#ff9966;">// 注意修饰符public static final都必须要有,名字CREATOR也是固定的,不能改成小写或其他名字</span>public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {@Overridepublic Student createFromParcel(Parcel in) {return new Student(in);}@Overridepublic Student[] newArray(int size) {return new Student[size];}};// 注意这个构造方法是私有的private Student(Parcel in) {<span style="color:#ff9966;">// 数据读取的顺序要和写入时保持一致</span>name = in.readString();age = in.readInt();}// 描叙内容,不用管@Overridepublic int describeContents() {return 0;}<span style="color:#ff9966;">// 将数据写入Parcel中,Parcel对象似于一个双向流,既可以写入又可以读取</span>@Overridepublic void writeToParcel(Parcel out, int flags) {out.writeString(name);out.writeInt(age);}}
Parcelable是一种类似于Serializable的序列化接口,效率比Serializable略高效,但不能使用Parcelable将数据存储在磁盘上,而只能序列化到内存中(只能代替DataOutputStream和DataInputStream),因为Parcelable不能很好的保证数据的持续性,所以Parcelable主要用于Intent数据传递,也可以用于进程间通信。
0 0
- Parcelable接口介绍与使用
- Android Parcelable接口使用介绍
- Parcelable接口使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- 完全二叉树和满二叉树的区别
- iOS-UITableView 滚动到底部的方法
- Android设计风向标Material design语言
- IOS开发之多态
- Linux系统下可执行文件的运行过程
- Parcelable接口介绍与使用
- JAVA字符串谜题
- 关于线程的一点总结
- 模版方法(Template Method)——类行为型模式
- shell函数
- 添加Structs和Spring支持-----SSH框架搭建2
- DEDE模板中如何使用if进行多分支判断
- iOS复制内容到剪切版
- Python基础:常用函数