Java 中的 putSerializable 和 putParcelable 有什么不同?
What's different of putSerializable and putParcelable in Java?
您好,我是 Android 的开发新手 :) 我想将 ArrayList<Custom>
值从 Activity
发送到 Fragment
。我知道必须使用Bundle
,但我不知道有什么方法可以帮助我。
putSerializable
和 putParcelable
有什么不同?
- 在我的情况下,哪种方法可以帮助我
putParcelableArrayList
或 putSerializable
?
感谢所有给我成长机会的人:)
一般来说,Parcelable
比 Serializable
快得多。如果您想了解更多,这里有一篇很棒的文章 http://www.developerphil.com/parcelable-vs-serializable/
Serializable
和 Parcelable
都是接口。前者与java一样古老。它允许您以持久的方式 store/write 一个对象。它的美妙之处在于你只需要实现接口。后者 Parcelable
是 android 特定的。它比 Serializable
快,但您必须实现接口的方法才能使用它。
What is different of putSerializable and putParcelable
putSerializable
接受实现Serializable
的对象,putParcelable接受实现Parcelable
的对象
In my situation, Which method can help me putParcelableArrayList or
putSerializable?
很难说。一般来说我会说 Parcelable
putSerializable 和 putParcelable 之间除了为给定键添加到地图的实际值外没有区别。 putParcelableArrayList 也是如此。在给定的键你会读到 SparseArray<? extends Parcelable>
.
Blackbelt 解释得很好。
在我看来,您不需要在内部使用 Serializable。
我建议使用 android 的消息容器(Parceable)。你永远不知道 Android SDK 会去哪里,你确实想轻松确保最大的兼容性。请记住,Android 有自己的 SDK(不是 Java 的)。
您必须实施 Parcelable
或 Serializable
class,两者都允许持久化。
Parcelable
更快,您将编写更多函数使其工作:
describeContents()
writeToParcel(Parcel out, int flags)
Parcelable.Creator<MyClass> CREATOR = new Parcelable.Creator<MyClass>()
MyClass(Parcel in) //an constructor for Parcel object (Parcelable)
http://developer.android.com/reference/android/os/Parcelable.html
Serializable
较低,但您只需在 class 上添加接口即可使其工作:
private static final long serialVersionUID = 0L;
应该设置在实现 Serializable 的 class 中
http://developer.android.com/reference/java/io/Serializable.html
Parcelable in android 是一个接口,每个想要使用 intent 跨不同活动传递的对象都必须实现这个 interface.This 接口有两个方法,我们必须 implements: describeContent()
returns 一个 int 和 writeToParcel(Parcel dest, int flags)
returns 一个 void。此外,实现此接口的 class 必须有一个名为 CREATOR
的静态字段,OS 使用它来重新创建 object.Clearly Parcelable 数组可以通过 Intent in android。这是在活动之间传递数据并利用所有 android 功能的最优雅方式。
Serialization 通常在需要通过网络发送数据或存储在文件中时使用。
您好,我是 Android 的开发新手 :) 我想将 ArrayList<Custom>
值从 Activity
发送到 Fragment
。我知道必须使用Bundle
,但我不知道有什么方法可以帮助我。
putSerializable
和putParcelable
有什么不同?- 在我的情况下,哪种方法可以帮助我
putParcelableArrayList
或putSerializable
?
感谢所有给我成长机会的人:)
一般来说,Parcelable
比 Serializable
快得多。如果您想了解更多,这里有一篇很棒的文章 http://www.developerphil.com/parcelable-vs-serializable/
Serializable
和 Parcelable
都是接口。前者与java一样古老。它允许您以持久的方式 store/write 一个对象。它的美妙之处在于你只需要实现接口。后者 Parcelable
是 android 特定的。它比 Serializable
快,但您必须实现接口的方法才能使用它。
What is different of putSerializable and putParcelable
putSerializable
接受实现Serializable
的对象,putParcelable接受实现Parcelable
In my situation, Which method can help me putParcelableArrayList or putSerializable?
很难说。一般来说我会说 Parcelable
putSerializable 和 putParcelable 之间除了为给定键添加到地图的实际值外没有区别。 putParcelableArrayList 也是如此。在给定的键你会读到 SparseArray<? extends Parcelable>
.
Blackbelt 解释得很好。
在我看来,您不需要在内部使用 Serializable。 我建议使用 android 的消息容器(Parceable)。你永远不知道 Android SDK 会去哪里,你确实想轻松确保最大的兼容性。请记住,Android 有自己的 SDK(不是 Java 的)。
您必须实施 Parcelable
或 Serializable
class,两者都允许持久化。
Parcelable
更快,您将编写更多函数使其工作:
describeContents()
writeToParcel(Parcel out, int flags)
Parcelable.Creator<MyClass> CREATOR = new Parcelable.Creator<MyClass>()
MyClass(Parcel in) //an constructor for Parcel object (Parcelable)
http://developer.android.com/reference/android/os/Parcelable.html
Serializable
较低,但您只需在 class 上添加接口即可使其工作:
private static final long serialVersionUID = 0L;
应该设置在实现 Serializable 的 class 中
http://developer.android.com/reference/java/io/Serializable.html
Parcelable in android 是一个接口,每个想要使用 intent 跨不同活动传递的对象都必须实现这个 interface.This 接口有两个方法,我们必须 implements: describeContent()
returns 一个 int 和 writeToParcel(Parcel dest, int flags)
returns 一个 void。此外,实现此接口的 class 必须有一个名为 CREATOR
的静态字段,OS 使用它来重新创建 object.Clearly Parcelable 数组可以通过 Intent in android。这是在活动之间传递数据并利用所有 android 功能的最优雅方式。
Serialization 通常在需要通过网络发送数据或存储在文件中时使用。