I have a question, why we using Parcelable/Serializable when passing data between activities. As I read in the documents around internet, Parcelable/Serializable is used when we need to pass data between processes. But all the activities of the same application run on the same process. Even Service started by an Activity run on the same process too. Then why we need to implement Parcelable/Serializable in this case ?
Asked
Active
Viewed 125 times
0
-
Activity vs Service : Same process doesn't mean the same thread. Activity vs Activity : The life cycle of an Activity to another is completely independent. The only way to communicate is with a Budle. – lopez.mikhael Oct 08 '15 at 07:27
-
As long as it's on the same process, that's mean that all the components share the same memory. So it doesn't matter if it's on another thread. – Nguyen Quang Anh Oct 08 '15 at 07:28
-
The only way to update an object from another is that it is instantiated by the first. In this case this isn't, it is the framework that manages the instances of the activity and services. – lopez.mikhael Oct 08 '15 at 07:31
-
*"Even Service started by an Activity run on the same process too"* - by default, yes, [but you can instruct otherwise](http://developer.android.com/guide/topics/manifest/service-element.html#proc). Whoever starts the service cannot (and should not) make assumptions about which process it may run in. Same goes for activities: *your* activities will run in your app's own process, but think about what happens when you start an *implicit* intent that isn't handled by your own app, say to view a url, or a contact... – MH. Oct 08 '15 at 07:41
-
@lopez.mikhael So you mean that we have to do that, because Android Sdk tells us so right ? But theoretically, what I said is true, means that all components do share the same memory right ? Because earlier when I do some research on this matter about shared memory in Android, I run across some interesting post, where people use C++ on Android, and create a shared memory zone. – Nguyen Quang Anh Oct 08 '15 at 07:42
-
@MH. Of course in that case, we should use Parcelable / Serializable. But here I was talking about those that run on the same process. Say for example, 2 activities run on the same process, isn't it more easier to pass object's reference between each other, than convert it into bit stream, then build an object back on another head using those datas. – Nguyen Quang Anh Oct 08 '15 at 07:49
-
@NguyenQuangAnh: Isn't that question already answered below? There is nothing preventing you from having some sort of singleton somewhere (heck, your `Application` *is* one) to share data between (activity) instances. I'd even bet this is a common pattern in Android apps, especially for mutable data. There is really no one forcing you to bundle up parameters into an intent. As a side-effect, you will have to manually handle saving and restoring data your activity relies on of course, as there are no guarantees about when it may or may not be killed by the system. – MH. Oct 08 '15 at 08:00
-
@MH. thanks. I get it somehow. Then the reason why we use Parcelable / Serializable is not because we have to, but to ensure some kinds of interruptions. I just wanna make sure that my tiny knowledge about system is still true. in some senses. – Nguyen Quang Anh Oct 08 '15 at 08:14
1 Answers
0
The app containing the parcel receiving activity might get destroyed and later be recreated so the data must be persisted to some kind of temp file.
Example:
- parcelSenderActivity s is active
- s calls parcelReceiverActivity r with parcable intent-extra
- r is interrupted due to a phone call,
- android removes app containing r and s from memory
- after phone call is finished r is recreated by reloading it with parcable intent-extra in a differend process/thread
this recreation is only possible if the intentparameter can be persisted to a femp-file.

k3b
- 14,517
- 7
- 53
- 85
-
ohhh so this `Intent.extra` is saved in the tempt-file ? I never paid attention to this before. So when this temp-file is created, and destroyed ? Would you tell me please ? Thanks – Nguyen Quang Anh Oct 08 '15 at 08:10