StartActivityForResult() 可能崩溃?

发布于 2024-11-30 18:55:53 字数 472 浏览 1 评论 0 原文

我有一个简单的问题,我希望其他人能快速回答:如果您启动活动 [A],然后使用 startActivityForResult() 启动活动 [B],当 [B] 完成并返回时,是否有可能[A] 可能已被 GC 回收的值(意图),因此一切都会崩溃?

我在某处读到这可能会发生,因此您不应该使用 Intent.putExtras() 传回重要信息,因为它可能会丢失?相反,人们建议在sharedPreferences中保存状态,并在从[A]到[B]时简单地使用startActivity(),然后在从[B]返回到[A]时再次使用startActivity()?

我真的很想避免因此而重新编码我正在进行的项目,但如果这可能是一个问题,我显然希望在发布之前解决它。

但是,如果这是可能的,您难道不能只在 [B] 中使用 [A] 中的一些静态引用,从而进行硬引用,并且不允许 GC 吗?

感谢您的帮助伙计们!

I have a quick question I hope someone else has a quick answer to: If you start activity [A] and then start activity [B] with startActivityForResult(), is there a chance that when [B] is finished and returning a value(Intent) that [A] could have been GC'd and thus everything crashes?

I read somewhere that this could happen, and thus you shouldn't pass vital information back using Intent.putExtras() because it could be lost? Instead people recommend saving state in sharedPreferences and simply using startActivity() when both going from [A] to [B], and then again when going back from [B] to [A]?

I'd really like to avoid recoding the project I'm on because of this, but if it could be an issue I would obviously like to take care of it before release.

However, if this is possible couldn't you just put a few static references from [A] that you use in [B] and thus making a hard reference, and not allowing GC?

Thanks for any help guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

赴月观长安 2024-12-07 18:55:53

如果A已被破坏,它将被重新创建。来自 Android 开发指南中的 Activity 文档

protected void onActivityResult(int requestCode,int resultCode,Intent data)

[..]
当您的活动重新开始时,您将在 onResume() 之前立即收到此调用。

这意味着 Activity A 将正常重新创建(执行 onCreate()、onStart(),然后执行 onActivityResult(),如 Activity 生命周期),并且您在 Activity B 中设置的结果 Intent 将被传递。

If A has been destroyed, it will be recreated. From the Activity documentation in the Android Dev Guide:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

[..]
You will receive this call immediately before onResume() when your activity is re-starting.

That means Activity A will be recreated normally (executing onCreate(), onStart() and then onActivityResult(), as described in the Activity lifecycle) and the result Intent you set in Activity B will be passed.

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