何时将数据保存到数据库,onPause() 或 onStop()? [英] When to save data to database, onPause() or onStop()?

查看:31
本文介绍了何时将数据保存到数据库,onPause() 或 onStop()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了一百万次,我自己虽然我已经知道答案,正确的答案是唯一有保证的调用是 onPause(),所以你应该在那里保存你的数据.

I know this question has been asked a million times, I myself though that I already knew the answer and that the correct one was that the only guaranteed call is to onPause(), so you should save your data there.

但是,在android文档的很多地方,他们总是建议不要在onPause()方法中做繁重的工作(例如在数据库中写入数据),因为这会延迟活动之间的转换.

However, in many places of android documentation they always suggest not doing heavy work (such as writing data in database) in the onPause() method as it will delay the transition between the activities.

根据表 1 中的 Android 开发者指南

onPause():此方法通常用于将未保存的更改提交到持久数据、停止动画和其他可能消耗 CPU 的内容等.它应该很快地做任何它所做的事情,因为下一个活动在它返回之前不会被恢复.

onPause(): This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

可杀死:是

然后根据类似表格中的Android开发者参考指南.

它说的是同样的事情,但是:

It says the same thing but:

可杀死:蜂巢前

他们添加了一个小字条,上面写着:

And they add a little note that says:

请注意,针对以 HONEYCOMB 开头的平台的应用程序与针对先前平台的应用程序之间的这些语义会略有不同.从 Honeycomb 开始,应用程序在其 onStop() 返回之前不会处于可终止状态.这会影响 onSaveInstanceState(Bundle) 何时可以被调用(它可以在 onPause() 之后安全地调用,并允许应用程序安全地等待直到 onStop() 以保存持久状态.

Be aware that these semantics will change slightly between applications targeting platforms starting with HONEYCOMB vs. those targeting prior platforms. Starting with Honeycomb, an application is not in the killable state until its onStop() has returned. This impacts when onSaveInstanceState(Bundle) may be called (it may be safely called after onPause() and allows and application to safely wait until onStop() to save persistent state.

<小时>

可杀

注意上表中的Killable"列——对于那些被标记为可杀死的方法,之后该方法返回托管活动的进程可能会被系统随时杀死正在执行的另一行代码.

Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed.

对于 POST-HONEYCOMB(我不关心早期版本):那么,是否可以假设任何 Android 设备(包括不同的 ROM)都可以确保在 Activity 上调用 onStop?这是进行任何耗时的应用程序存储写入的最佳位置?

FOR POST-HONEYCOMB (i dont care about earlier versions): So, is it OK to assume that any Android device (including different ROMS) will ensure a call to onStop on the activity? And this is the best place to make any time consuming storage writing of the App?

注意:这非常令人困惑,因为这里的大多数答案、网站、书籍甚至在线 android 测试都将其作为正确答案,您应该将其保存在 onPause 而不是 onStop.

推荐答案

何时将数据保存到数据库,onPause() 或 onStop()?

When to save data to database, onPause() or onStop()?

要么.它们几乎相同,尤其是在 Android 3.0+ 上.

Either. They are nearly identical, particularly on Android 3.0+.

如果接管前台的 Activity 是典型的全屏 Activity,以至于前面的 Activity 不再可见,onPause()onStop() 将被快速连续调用.

If the activity that is taking over the foreground is a typical full-screen activity, so that the earlier activity is no longer visible, onPause() and onStop() will be called in rapid succession.

如果接管前台的 Activity 的主题更像是一个对话框,其中较早的 Activity 仍然可见,onPause() 将被调用,但不会调用 onStop(),直到活动不再可见(例如,用户现在按下 HOME).

If the activity that is taking over the foreground is themed to be more like a dialog, where the earlier activity is still visible, onPause() will be called, but not onStop(), until such time as the activity is no longer visible (e.g., user now presses HOME).

大多数应用程序并不担心主题更像是一个对话框"的情况,在这种情况下 onPause()onStop() 被称为正确的在下一个之后,您可以分叉您的后台线程以将您的数据保存在对您有意义的任何一个中.

Most apps aren't worried about the "themed to be more like a dialog" scenario, in which case onPause() and onStop() are called one right after the next, and you can fork your background thread to save your data in whichever of those makes sense to you.

但是,在android文档的很多地方,他们总是建议不要在onPause()方法中做繁重的工作(例如在数据库中写入数据),因为这会延迟活动之间的转换.

However, in many places of android documentation they always suggest not doing heavy work (such as writing data in database) in the onPause() method as it will delay the transition between the activities.

onStop() 也是如此,因为这两个方法都是在主应用程序线程上调用的.

The same is true of onStop(), as both of those methods are called on the main application thread.

那么,是否可以假设任何 Android 设备(包括不同的 ROM)都会确保在 Activity 上调用 onStop?

So, is it OK to assume that any Android device (including different ROMS) will ensure a call to onStop on the activity?

从进程终止的角度来看,onPause()onStop() 将具有相同的特性.要么两个都被调用(正常情况下),要么都不调用(例如,你崩溃了,电池从手机背面弹出).

Both onPause() and onStop() will have the same characteristics from the standpoint of process termination. Either both should be called (normal case) or neither will be called (e.g., you crash, the battery pops out the back of the phone).

这是进行任何耗时的应用程序存储写入的最佳位置吗?

And this is the best place to make any time consuming storage writing of the App?

onPause()onStop() 都是触发工作的好地方,在后台线程上完成,保存数据.如果您更喜欢在 onStop() 中完成这项工作,绝对欢迎您这样做.就个人而言,我是一个 onPause() 类型的人.

Either onPause() or onStop() are fine places to trigger the work, done on a background thread, to persist your data. If you prefer to do that work in onStop(), you are absolutely welcome to do so. Personally, I'm an onPause() kind of guy.

这篇关于何时将数据保存到数据库,onPause() 或 onStop()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆