是否必须删除 Intent extra?

发布于 2024-11-09 05:44:15 字数 579 浏览 3 评论 0原文

这可能是一个愚蠢的问题,但是是否有一条规则规定消费活动必须显式删除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2024-11-16 05:44:15

如果您计划使用相同的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文