blob: 448b591718f8ee714b766bd87021c52f35af2514 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
19/**
20 * Interface for classes whose instances can be written to
21 * and restored from a {@link Parcel}. Classes implementing the Parcelable
Neil Fuller44e440c2015-04-20 14:39:00 +010022 * interface must also have a non-null static field called <code>CREATOR</code>
23 * of a type that implements the {@link Parcelable.Creator} interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024 *
25 * <p>A typical implementation of Parcelable is:</p>
26 *
27 * <pre>
28 * public class MyParcelable implements Parcelable {
29 * private int mData;
Bjorn Bringertd8919f02009-10-28 19:16:24 +000030 *
31 * public int describeContents() {
32 * return 0;
33 * }
34 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 * public void writeToParcel(Parcel out, int flags) {
36 * out.writeInt(mData);
37 * }
38 *
Bjorn Bringertd8919f02009-10-28 19:16:24 +000039 * public static final Parcelable.Creator&lt;MyParcelable&gt; CREATOR
40 * = new Parcelable.Creator&lt;MyParcelable&gt;() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 * public MyParcelable createFromParcel(Parcel in) {
42 * return new MyParcelable(in);
43 * }
44 *
45 * public MyParcelable[] newArray(int size) {
46 * return new MyParcelable[size];
47 * }
Bjorn Bringertd8919f02009-10-28 19:16:24 +000048 * };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 *
50 * private MyParcelable(Parcel in) {
51 * mData = in.readInt();
52 * }
53 * }</pre>
54 */
55public interface Parcelable {
56 /**
57 * Flag for use with {@link #writeToParcel}: the object being written
58 * is a return value, that is the result of a function such as
59 * "<code>Parcelable someFunction()</code>",
60 * "<code>void someFunction(out Parcelable)</code>", or
61 * "<code>void someFunction(inout Parcelable)</code>". Some implementations
62 * may want to release resources at this point.
63 */
64 public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;
65
66 /**
67 * Bit masks for use with {@link #describeContents}: each bit represents a
68 * kind of object considered to have potential special significance when
69 * marshalled.
70 */
71 public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;
72
73 /**
74 * Describe the kinds of special objects contained in this Parcelable's
75 * marshalled representation.
76 *
77 * @return a bitmask indicating the set of special object types marshalled
78 * by the Parcelable.
79 */
80 public int describeContents();
81
82 /**
83 * Flatten this object in to a Parcel.
84 *
85 * @param dest The Parcel in which the object should be written.
86 * @param flags Additional flags about how the object should be written.
87 * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
88 */
89 public void writeToParcel(Parcel dest, int flags);
90
91 /**
92 * Interface that must be implemented and provided as a public CREATOR
93 * field that generates instances of your Parcelable class from a Parcel.
94 */
95 public interface Creator<T> {
96 /**
97 * Create a new instance of the Parcelable class, instantiating it
98 * from the given Parcel whose data had previously been written by
99 * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
100 *
101 * @param source The Parcel to read the object's data from.
102 * @return Returns a new instance of the Parcelable class.
103 */
104 public T createFromParcel(Parcel source);
105
106 /**
107 * Create a new array of the Parcelable class.
108 *
109 * @param size Size of the array.
110 * @return Returns an array of the Parcelable class, with every entry
111 * initialized to null.
112 */
113 public T[] newArray(int size);
114 }
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700115
116 /**
117 * Specialization of {@link Creator} that allows you to receive the
118 * ClassLoader the object is being created in.
119 */
120 public interface ClassLoaderCreator<T> extends Creator<T> {
121 /**
122 * Create a new instance of the Parcelable class, instantiating it
123 * from the given Parcel whose data had previously been written by
124 * {@link Parcelable#writeToParcel Parcelable.writeToParcel()} and
125 * using the given ClassLoader.
126 *
127 * @param source The Parcel to read the object's data from.
128 * @param loader The ClassLoader that this object is being created in.
129 * @return Returns a new instance of the Parcelable class.
130 */
131 public T createFromParcel(Parcel source, ClassLoader loader);
132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133}