Reputation:
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!
Upvotes: 3
Views: 1589
Reputation: 3134
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.
Upvotes: 1
Reputation: 6866
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.
Upvotes: 5
Reputation: 907
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.
how do i pass data between activities?
Upvotes: -1