交互协调:onStop 和 onResume
我有两项活动。一个从数据库加载所有行,另一个保存到数据库。当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一个 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.
一些官方引用作为附加组件:
Activity 文档指出
Some official quote as an add-on:
The Activity documentation states that