我有一个简单的问题,我希望其他人能快速回答:如果您启动活动 [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!
发布评论
评论(1)
如果A已被破坏,它将被重新创建。来自 Android 开发指南中的 Activity 文档:
这意味着 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:
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.