1

I'm not asking how do I use it nor what does it do, how does it works. The question came to me when I though why didn't they just put a putExtra(String,Object) so I can pass an object. Obviously they just didn't forgot to do it, rather than the way Bundle works isn't one you can just do that.

PS: Serializable or Parcelable is something you cannot implement on every class you create, so they are not a replacement for putExtra(String,Object)

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206

4 Answers4

3

Obviously they just didn't forgot to do it

Correct.

A Bundle itself is Parcelable, as Doctoror Drive notes. The point behind a Parcelable is to be able to place it into a Parcel, and the point behind a Parcel is to pass the data across process boundaries. You cannot pass arbitrary objects across process boundaries, just as you cannot write arbitrary objects to a file and cannot stream arbitrary objects over a socket.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So the thing is you're actually passing data between processes. I had the idea that you were just passing data between activity subclasses, not processes themselves. Is it considered "hack-ish" to implement `Parcelable` on model(or entity) classes to pass them between activities rather than passing the ID and querying a database? – Christopher Francisco May 30 '14 at 12:32
  • @ChristopherFrancisco: "I had the idea that you were just passing data between activity subclasses, not processes themselves" -- it is possible that the sender and receiver are both in the same process, but usually the `Parcel` goes by way of another process. For example, all `startActivity()` calls go by way of an OS process, even if you are starting one of your own activities. With regards to "hack-ish", I tend to recommend pass-by-reference (ID) versus pass-by-copy (`Parcelable`), though both have their places. – CommonsWare May 30 '14 at 12:36
1

Basically, a parcelable or serializable class are "transformed" in generic binaries with your package reference. This able you to transfer and persist data over databases, Intents and more.

The idea behind this is keep the state of some Activity or Fragment for example as a state machine.

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.

Read more at Recreating an Activity.

Serializable

By default, the serialization mechanism encodes an object's class name, the names of its non-transient fields (including non-public fields), and the values of all of those fields. The output is an opaque sequence of bytes. Those bytes can be decoded into a new, equivalent instance as long as the decoder has compatible versions of the originating classes. Changing the class name, field names or field types breaks serialization compatibility and complicates interoperability between old and new versions of the serializable class. Adding or removing fields also complicates serialization between versions of a class because it requires your code to cope with missing fields.

Read more at: http://developer.android.com/reference/java/io/Serializable.html

Parcel

The bulk of the Parcel API revolves around reading and writing data of various types.

Read more at: http://developer.android.com/reference/android/os/Parcel.html and http://developer.android.com/reference/android/os/Parcelable.html

Bundle documentation: http://developer.android.com/reference/android/os/Bundle.html

More links and posts

Hope helped.

Community
  • 1
  • 1
Idemax
  • 2,712
  • 6
  • 33
  • 66
0

Bundle implements Parcelable so all the objects you pass must be valid for a Parcel, otherwise. Bundle can't be used passed as a Parcelable.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
0

I'm not asking how do I use it nor what does it do, how does it works.

Bundle implements Parcelable, so it has to pass Objects to a Parcel. I would assume that Bundles are backed by HashMaps given their key, value nature.

Obviously they just didn't forgot to do it, rather than the way Bundle works isn't one you can just do that.

You are right. They have not forgot it. Bundles can be used to do IPC (Inter Process Communication), so the system needs to know how to recreate an Object, hence Bundle implements Parcelable. This makes you confine to the same paradigm. If you have a custom Object it has to be able to tell Android how to re-construct itself across processes; hence it needs to implement Parcelable.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53