android parcelable 详细介绍
生活随笔
收集整理的這篇文章主要介紹了
android parcelable 详细介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????? 想要在兩個activity之間傳遞對象,那么這個對象必須序列化,Android中序列化一個對象有兩種方式,一種是實現Serializable接口,這個非常簡單,只需要聲明一下就可以了,不痛不癢。但是android中還有一種特有的序列化方法,那就是實現Parcelable接口,使用這種方式來序列化的效率要高于實現Serializable接口。不過Serializable接口實在是太方便了,因此在某些情況下實現這個接口還是非常不錯的選擇。?
使用Parcelable步驟:?
1.實現Parcelable接口?
2.實現接口中的兩個方法
- 1
- 2
- 1
- 2
第一個方法是內容接口描述,默認返回0就可以了?
第二個方法是將我們的對象序列化一個Parcel對象,也就是將我們的對象存入Parcel中?
3.實例化靜態內部對象CREATOR實現接口Parcelable.Creator,實例化CREATOR時要實現其中的兩個方法,其中createFromParcel的功能就是從Parcel中讀取我們的對象。
也就是說我們先利用writeToParcel方法寫入對象,再利用createFromParcel方法讀取對象,因此這兩個方法中的讀寫順序必須一致,否則會出現數據紊亂,一會我會舉例子。?
看一個代碼示例:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
最后貼上Parcelable源碼,Google已經給了一個示例了:
/** Copyright (C) 2006 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.os;/*** Interface for classes whose instances can be written to* and restored from a {@link Parcel}. Classes implementing the Parcelable* interface must also have a static field called <code>CREATOR</code>, which* is an object implementing the {@link Parcelable.Creator Parcelable.Creator}* interface.* * <p>A typical implementation of Parcelable is:</p>* * <pre>* 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();* }* }</pre>*/ public interface Parcelable {/*** Flag for use with {@link #writeToParcel}: the object being written* is a return value, that is the result of a function such as* "<code>Parcelable someFunction()</code>",* "<code>void someFunction(out Parcelable)</code>", or* "<code>void someFunction(inout Parcelable)</code>". Some implementations* may want to release resources at this point.*/public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;/*** Bit masks for use with {@link #describeContents}: each bit represents a* kind of object considered to have potential special significance when* marshalled.*/public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;/*** Describe the kinds of special objects contained in this Parcelable's* marshalled representation.* * @return a bitmask indicating the set of special object types marshalled* by the Parcelable.*/public int describeContents();/*** Flatten this object in to a Parcel.* * @param dest The Parcel in which the object should be written.* @param flags Additional flags about how the object should be written.* May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.*/public void writeToParcel(Parcel dest, int flags);/*** Interface that must be implemented and provided as a public CREATOR* field that generates instances of your Parcelable class from a Parcel.*/public interface Creator<T> {/*** Create a new instance of the Parcelable class, instantiating it* from the given Parcel whose data had previously been written by* {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.* * @param source The Parcel to read the object's data from.* @return Returns a new instance of the Parcelable class.*/public T createFromParcel(Parcel source);/*** Create a new array of the Parcelable class.* * @param size Size of the array.* @return Returns an array of the Parcelable class, with every entry* initialized to null.*/public T[] newArray(int size);}/*** Specialization of {@link Creator} that allows you to receive the* ClassLoader the object is being created in.*/public interface ClassLoaderCreator<T> extends Creator<T> {/*** Create a new instance of the Parcelable class, instantiating it* from the given Parcel whose data had previously been written by* {@link Parcelable#writeToParcel Parcelable.writeToParcel()} and* using the given ClassLoader.** @param source The Parcel to read the object's data from.* @param loader The ClassLoader that this object is being created in.* @return Returns a new instance of the Parcelable class.*/public T createFromParcel(Parcel source, ClassLoader loader);} } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的android parcelable 详细介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVP介绍以及优化封装
- 下一篇: ECB模式详解