交互协调:onStop 和 onResume

发布于 2024-09-27 22:48:17 字数 153 浏览 3 评论 0原文

我有两项活动。一个从数据库加载所有行,另一个保存到数据库。当我在 onStop 中进行第二次保存并在 onResume 中进行第一次重新提取数据时,它们是无序的(第一次恢复,然后第二次保存)。我设法通过将保存数据放入 onPause 来解决此问题,但为什么会发生这种情况?这是最干净的方法吗?

I have two activities. One loads all rows from a database, and the other saves to the database. When I have the second save in onStop and the first repull the data in onResume, they do it out of order (the first resumes and then the second saves). I managed to fix this by putting the saving data in onPause, but why was this happening? Was this the cleanest way to do it?

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

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

发布评论

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

评论(2

眼泪也成诗 2024-10-04 22:48:17

在第一个 Activity 的 onPause 中进行保存应该没问题。

您已经发现,活动的前台生命周期发生在对 onResume() 的调用和对 onPause() 的相应调用之间。在此期间,该 Activity 位于屏幕上所有其他 Activity 的前面并与用户进行交互。

当您启动第二个活动时,第一个活动将调用 onPause,然后交互式控制切换到第二个活动,第一个活动的 onStop 将在后台调用。

这提高了响应能力并尽快将新活动呈现在用户面前。因此,您应该尝试使 onPause 实现尽可能快速和高效。

有关生命周期的更多详细信息,请参阅以下 Android 文档 http://developer.android.com/ Guide/topics/fundamentals.html,但是您发现的内容应该适合您。

Doing the save in the first actvity's onPause should be fine.

You've discovered that the foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time, the activity is in front of all other activities on screen and is interacting with the user.

When you start the second activity, onPause is called on the first and then interactive control switches to the second, with onStop on the first to be called somewhat in background.

This improves responsiveness and gets the new activity in front of the user ASAP. Consequently, you should try to keep your onPause implementation as fast and efficient as possible.

See the following Android docs for more details on the lifecycle http://developer.android.com/guide/topics/fundamentals.html, but what you have found should work fine for you.

错々过的事 2024-10-04 22:48:17

一些官方引用作为附加组件:

Activity 文档指出

onPause() 是处理用户离开 Activity 的地方。
最重要的是,此时用户所做的任何更改都应该是
承诺

Some official quote as an add-on:

The Activity documentation states that

onPause() is where you deal with the user leaving your activity.
Most importantly, any changes made by the user should at this point be
committed

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