blob: 13b5de9e3a0d51505001b1f6f4c5a5344dc6e5f8 [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;
Christopher Tateb9116762015-09-09 18:46:31 -070065
66 /**
67 * Flag for use with {@link #writeToParcel}: a parent object will take
68 * care of managing duplicate state/data that is nominally replicated
69 * across its inner data members. This flag instructs the inner data
70 * types to omit that data during marshaling. Exact behavior may vary
71 * on a case by case basis.
72 * @hide
73 */
74 public static final int PARCELABLE_ELIDE_DUPLICATES = 0x0002;
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
77 * Bit masks for use with {@link #describeContents}: each bit represents a
78 * kind of object considered to have potential special significance when
79 * marshalled.
80 */
81 public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;
82
83 /**
84 * Describe the kinds of special objects contained in this Parcelable's
85 * marshalled representation.
86 *
87 * @return a bitmask indicating the set of special object types marshalled
88 * by the Parcelable.
89 */
90 public int describeContents();
91
92 /**
93 * Flatten this object in to a Parcel.
94 *
95 * @param dest The Parcel in which the object should be written.
96 * @param flags Additional flags about how the object should be written.
97 * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
98 */
99 public void writeToParcel(Parcel dest, int flags);
100
101 /**
102 * Interface that must be implemented and provided as a public CREATOR
103 * field that generates instances of your Parcelable class from a Parcel.
104 */
105 public interface Creator<T> {
106 /**
107 * Create a new instance of the Parcelable class, instantiating it
108 * from the given Parcel whose data had previously been written by
109 * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
110 *
111 * @param source The Parcel to read the object's data from.
112 * @return Returns a new instance of the Parcelable class.
113 */
114 public T createFromParcel(Parcel source);
115
116 /**
117 * Create a new array of the Parcelable class.
118 *
119 * @param size Size of the array.
120 * @return Returns an array of the Parcelable class, with every entry
121 * initialized to null.
122 */
123 public T[] newArray(int size);
124 }
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700125
126 /**
127 * Specialization of {@link Creator} that allows you to receive the
128 * ClassLoader the object is being created in.
129 */
130 public interface ClassLoaderCreator<T> extends Creator<T> {
131 /**
132 * Create a new instance of the Parcelable class, instantiating it
133 * from the given Parcel whose data had previously been written by
134 * {@link Parcelable#writeToParcel Parcelable.writeToParcel()} and
135 * using the given ClassLoader.
136 *
137 * @param source The Parcel to read the object's data from.
138 * @param loader The ClassLoader that this object is being created in.
139 * @return Returns a new instance of the Parcelable class.
140 */
141 public T createFromParcel(Parcel source, ClassLoader loader);
142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143}