Android Notepadv3教程 - onSaveInstanceState中的saveState()真的有必要吗?

发布于 2024-09-18 11:10:26 字数 418 浏览 7 评论 0原文

我只是 Android 的初学者,但我注意到第三个记事本教程中的一些事情看起来有点奇怪:

  • 该教程明确指出您需要从 onSaveInstanceState 和 onPause 调用 saveState。这真的有必要吗?在我看来,通过阅读进程生命周期文档,onPause 总是会在 Activity 被终止之前被调用,无论是由系统还是用户执行。如果是这种情况,那么仅仅从 onPause 调用 saveState 就足够了吗?
  • 在 onResume 和 onCreate 中调用 populateFields() 是没有意义的,因为无论如何 onResume 总是在 onCreate 之后调用。因此,如果我理解正确的话,onCreate 中的调用没有任何用处,可以安全地删除。

有人可以确认这些观点,或者让我知道我误解了什么。

奥利

I am only a beginner to Android, but I have noticed a number of things that seem a little strange in the third notepad tutorial:

  • The tutorial explicitly states that you need to call saveState from BOTH onSaveInstanceState and onPause. Is this really necessary? It seems to me from reading the process life-cycle documentation that onPause will always be called before the Activity is killed, regardless of whether it is done so by the system or the user. If this is the case, surely just calling saveState from onPause is sufficient?
  • Calling populateFields() in both onResume and onCreate is pointless, since onResume is always called after onCreate anyway. Hence if I understand correctly, the call in onCreate serves no useful purpose and can be safely removed.

Could someone please either confirm these points, or let me know what I have misunderstood.

Olly

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

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

发布评论

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

评论(1

唔猫 2024-09-25 11:10:26

据我了解...

您是正确的,onResume 最终总是在 onCreate 之后调用,并且您通常可以将代码放在那里。请记住,当活动返回前台时,onResume 也会被调用,而无需首先执行 onCreate。因此,对于初始化代码(如 setContentView() 或初始化变量)来说,onCreate 事件是一个更好的地方。

另外,您对 onPause 和 onStop 的看法是正确的。 onPause 是您通常编写状态处理代码的地方。 onPause 是可终止的,因此 onStop 在内存非常低的情况下甚至可能不会被调用。

From what I understand...

You are correct that onResume is always called eventually after onCreate, and you can often put the code there. Keep in mind though that onResume will also be called when an activity comes back to the foreground, without first going through onCreate. So for one time initialization code (like setContentView() or initializing variables) the onCreate event is a better place.

Also, you are correct about the onPause and onStop. onPause is where you'd normally code the state handling. onPause is killable so that onStop can potentially not even get called in very low memory situations.

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