I'm a C/C++ guy, and I'm quite new to Java and Android. So I'm creating a class instance in Activity "A" and I'm trying to use this instance to Activity "B". In C/C++ you would simply pass the address (pointer) of the instance. Now I learned that with Android I should use a Parcel, but I don't get the idea: why would I want do all that parceling/deparceling procedure when all I want to do is passing a pointer? Please enlighten me!
-
Refer this for parcel http://developer.android.com/reference/android/os/Parcel.html – Joy Helina Aug 21 '15 at 07:42
-
It's a design principle I suppose that activities need to be standalone and independent but what I don't understand is, why must it be so complicated and why a "serializable" object wouldn't suffice and instead we need to go through this pain of making parcelabel objects but that's another topic. Regarding your question, just think about activities as different processes which obviously can't share memory (in terms of C++) and as such, need a mechanism to pass things around. – kha Aug 21 '15 at 07:44
-
@kha, serializable is enough. Parce is faster and preffered. Plus there are several plugins for Android Studio that implement parce with one click so im not sure about the "pain" – poss Aug 21 '15 at 07:47
-
@poss can you please tell me one of the plugins? (I'm not being nasty btw, genuinely happy to hear of its existence!) I didn't know of their existence until your comment, hence the pain comment. I couldn't make serializable work (putExtra didn't work with it) but if there's an easy way to create parcelabels, i'll be very happy to use it. Manually creating them, as I have done so far, is truly painful and error prone. – kha Aug 21 '15 at 07:49
-
@kha I think I'm using this one, but can't check right now : https://plugins.jetbrains.com/plugin/7332?pr=androidstudio You should be able to search for "parce" or "parceable" directly from Studio to find them – poss Aug 21 '15 at 07:52
-
@poss great. tyvm. I'll do that when I'm at home. – kha Aug 21 '15 at 07:54
3 Answers
This is related to the Android activity model.
Since your app may be killed by the system to recover RAM (this should only happen when it's in the background, unless the system is in a really bad situation), an object reference just won't do. It'd be completely worthless once your process dies.
The intent you use to start activities is saved by the Activity Manager, so that it can be used to recreate your activity when the user navigates back to it. That's why all the data in your intent has to be parceled.
Keep in mind that this means that your two activities will have two similar instances -- but naturally, they will not be the same object, so changes to one won't be reflected in the other.

- 6,669
- 3
- 20
- 32
Parcel or parcalable is an efficient and alternative way to serialize objects in Android. Its an alternative to Serialization in core Java. Java Serialization API allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Its a way to exchange objects between sender and receiver(Class). Receiver than deserializes this object. In Android parcel is used to exchange objects between different activities by using Intents.

- 2,868
- 1
- 31
- 24
java doesn't work like c/c++, you don't work directly with the memory. If you want to pass an object from A to B, you have to set it in a Bundle, and add that bundle to the intent. if it's a custom class, let's call it C, C should implement parcellable. Java works by value, not by reference.
-
"Java works by value, not by reference." While this is technically true, it is misleading for a C++ programmer. Object variables are always references ("pointers"), so passing that as an arg will pass the reference -- Java will never copy your object when passing arguments (which "works by value" would imply to many C/C++ programmers). – Snild Dolkow Aug 21 '15 at 07:50
-
it's still no reason for a downvote, Android apps work on a level at wich memory pointers (with the same functionality in C/C++) are not only useless, but also not possible to achieve. – CptEric Aug 21 '15 at 07:53
-
1Oh, I didn't downvote because of that. I downvoted because your answer is irrelevant to the question: "*why* do I need to use a Parcel?". – Snild Dolkow Aug 21 '15 at 07:56