3

I've looked for an explanation for this and couldn't find one.

I was asked in a job interview why Android doesn't allow to pass any type through an intent.

I'm well aware of the possibilities - Serialization, implementing "Parcelable" interface, bundle etc.

I want to understand the rationale behind the decision to deny the passing of custom types/objects with an intent. I was led to understand that it was for security reasons, but I find these reasons.

2 Answers 2

6

The first reason is simply because the receiver of the intent will almost always be in a different process, which means a different jvm. Because process and applications are sandbox (meaning they are not allowed to see each other's memory), you need to send a description of the object through serialization (most usually parcelables in android), rather than sending the object instance (ie: a pointer to the memory where it is).

Another reason, as Kuffs said, is that intent can survive the lifespan of the object you want to send (especially with pending intents).

1
  • While the receiver can be in a different process, in actuality much of the time it will be in the same process as on of an App's Activities forwards to another, or interacts with one of its Services which most usually all run in the same process (though that is configurable). The mechanism is process agnostic, but that doesn't mean it will necessarily target a different process, apart from the system code which manages Intents. Nov 28, 2015 at 1:56
6

Custom objects have a limited lifecycle. They can contain variables etc that would be lost if the device was restarted.

All objects passed into an intent have the ability to be stored indefinitely because they can be serialized or flattened and their value(s) saved until such a time that they are needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.