2015-08-25 16 views
0

我不清楚Android中界面SerializableParcelable的不同用法。如果我們更喜歡使用Parcelable,那麼我們爲什麼要這麼做呢?Serializable。而且,如果我通過任何web服務傳遞數據,Parcelable可以提供幫助嗎?如果是這樣,那麼怎麼樣?Android中的可串行化和可分段化

+1

可能重複檢查這個http://stackoverflow.com/questions/3323074/android-difference-between- parcelable和序列化 –

回答

2

所以主要區別在於速度,它對手持設備很重要。據說功能不如桌面。

隨着Serialisable其中談到了古爾德的舊Java,您的代碼將是這樣的:需要

class MyPojo implements Serializable { 
    String name; 
    int age; 
} 

沒有額外的方法如reflection將被用來獲取所有字段及其值。這會我慢或慢於...

,並使用Parcelable你必須寫是這樣的:根據傢伙

class MyPojo implements Parcelable { 
    String name; 
    int age; 

    MyPojo(Parcel in) { 
     name = in.readString(); 
     age = in.readInt(); 
    } 

    void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(name); 
     dest.writeInt(age); 
    } 

    int describeContents() { 
     return 0; 
    } 

    // and here goes CREATOR as well and what not 
}; 

@google它可以更快得多,Parcelable s表示是。

當然,也有工具來幫助您將所有必要的字段,使一類Parcelablehttps://github.com/johncarl81/parceler