是否必须删除 Intent extra?
这可能是一个愚蠢的问题,但是是否有一条规则规定消费活动必须显式删除 Intent 额外内容,或者只有在回收 Intent 对象时才如此?
换句话说,如果我总是通过执行以下操作来链接到下一个活动:
Intent i = new Intent(MyCurrentActivity.this, FooActivity.class);
i.putExtra("first", "stringvalue");
i.putExtra("second", 69L);
startActivity(i);
那么,在 FooActivity 中,我将它们读回来……
String first = getIntent().getStringExtra("first");
long second = getIntent().getLongExtra("second");
我是否还必须显式删除它们以避免意外污染未来活动的意图,或者从我抓住它们的那一刻起,我可以忘记它们的存在并继续前进吗?
我可以发誓我记得读过一些东西说我必须删除它们,但我无法再次找到它,而且我怀疑它可能只适用于重用的意图对象。
This might be a stupid question, but is there a rule that states that intent extras have to be explicitly removed by the consuming activity, or is that only true if you're recycling Intent objects?
Put another way, if I always chain to the next activity by doing something like:
Intent i = new Intent(MyCurrentActivity.this, FooActivity.class);
i.putExtra("first", "stringvalue");
i.putExtra("second", 69L);
startActivity(i);
then, in FooActivity, I read them back out...
String first = getIntent().getStringExtra("first");
long second = getIntent().getLongExtra("second");
... do I have to also explicitly remove them to avoid accidentally polluting a future activity's intent, or from the moment I finish grabbing them, can I just forget they even exist and move on?
I could swear I remember reading something that said I had to remove them, but I haven't been able to find it again, and I'm suspecting that it might only apply to reused intent objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您计划使用相同的 Intent 对象但不需要(也不想要)额外的内容,那么您可以删除它们。相反,如果您想以相同的意图和相同的附加功能来调用启动,则保留它们。最后,如果对象要被销毁,谁会关心额外的东西呢?
无论如何,我将决定调用者的活动,而不是意图的接收者。
If you're planning to use the same Intent object but do not need (nor want) the extras, then you may remove them. If, instead, you want to call the start the same intent with the same extras, then keep them. Lastly, if the object is going to be destroyed, who cares about the extras?
In any case, I would decide on the caller activity, not on the receiver of the intent.