Parcelable
public interface Parcelable
android.os.Parcelable |
Interface for classes whose instances can be written to and restored from a Parcel
. Classes implementing the Parcelable interface must also have a non-null static field called CREATOR
of a type that implements the Parcelable.Creator
interface.
接口类的实例可以写入和恢复从一个包裹。实现Parcelable接口的类必须有一个非空静态字段叫CREATOR 的实现Parcelable.Creator接口的类型。
A typical implementation of Parcelable is:
public class MyParcelable implements Parcelable { private int mData; public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeInt(mData); } public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { public MyParcelable createFromParcel(Parcel in) { return new MyParcelable(in); } public MyParcelable[] newArray(int size) { return new MyParcelable[size]; } }; private MyParcelable(Parcel in) { mData = in.readInt(); } }
Summary
Nested classes | |
---|---|
interface | Parcelable.ClassLoaderCreator<T> Specialization of 序列化Parcelable.Creator让你来接收类加载器中创建的对象。 |
interface | Parcelable.Creator<T> Interface that must be implemented and provided as a public CREATOR field that generates instances of your Parcelable class from a Parcel. 必须实现的接口,并提供了作为一个公共的 CREATOR Parcelable类的字段生成实例从一个包裹。 |
Constants | |
---|---|
int | CONTENTS_FILE_DESCRIPTOR Descriptor bit used with |
int | PARCELABLE_WRITE_RETURN_VALUE Flag for use with |
Public methods | |
---|---|
abstract int | describeContents() //内容描述接口,基本不用管 Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
abstract void | writeToParcel(Parcel dest, int flags) Flatten this object in to a Parcel. 这个对象在一个包裹。 |
Constants
CONTENTS_FILE_DESCRIPTOR
int CONTENTS_FILE_DESCRIPTOR
Descriptor bit used with describeContents()
: indicates that the Parcelable object's flattened representation includes a file descriptor.
See also:
Constant Value: 1 (0x00000001)
PARCELABLE_WRITE_RETURN_VALUE
int PARCELABLE_WRITE_RETURN_VALUE
Flag for use with writeToParcel(Parcel, int)
: the object being written is a return value, that is the result of a function such as "Parcelable someFunction()
", "void someFunction(out Parcelable)
", or "void someFunction(inout Parcelable)
". Some implementations may want to release resources at this point.
Constant Value: 1 (0x00000001)
Public methods
describeContents
int describeContents ()
Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(Parcel, int)
, the return value of this method must include the CONTENTS_FILE_DESCRIPTOR
bit.
Returns | |
---|---|
int | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. |
See also:
writeToParcel
void writeToParcel (Parcel dest, int flags)
Flatten this object in to a Parcel.
Parameters | |
---|---|
dest | Parcel : The Parcel in which the object should be written. |
flags | int : Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE . |