The rationale behind Android intent system

2020-07-22 18:15发布

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条回答
迷人小祖宗
2楼-- · 2020-07-22 18:26

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).

查看更多
Root(大扎)
3楼-- · 2020-07-22 18:32

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.

查看更多
登录 后发表回答