什么时候存数据到数据库,onPause() 还是onStop()?
When to save data to database, onPause() or onStop()?
我知道这个问题已被问过一百万次,尽管我自己已经知道答案并且正确的答案是唯一有保证的调用是 onPause(),因此您应该将数据保存在那里。
然而,在 android 文档的许多地方,他们总是建议不要在 onPause() 方法中做繁重的工作(例如在数据库中写入数据),因为它会延迟活动之间的转换。
根据Android Developer Guide in Table 1
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.
Killable: YES
然后根据Android Developer Reference Guide in the similar table.
它说的是同样的事情但是:
Killable: Pre-HONEYCOMB
然后他们添加了一个小注释,上面写着:
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.
可杀
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.
FOR POST-HONEYCOMB(我不关心早期版本):
那么,是否可以假设任何 Android 设备(包括不同的 ROM)将确保在 activity 上调用 onStop?而这里是让任何耗时的存储写入App的最佳位置?
注意:这非常令人困惑,因为这里的大多数答案、网站、书籍,甚至在线 android 测试都将正确答案视为正确答案,您应该将其保存在 onPause 而不是 onStop .
- 如果您想要更安全,请存储在
onPause
。
- 如果你的数据太大,需要保存几秒,可以打开后台
Service
(如IntentService
)保存。
- 您还可以在代码中检查系统版本并选择ose 保存时间。
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){}
- 在 most 种情况下,这个何时保存的规则不会被一些习惯 os 改变。但是当然可以有一些其他 os 肯定会改变它。所以在 android 开发中最重要的 os 是你需要知道在不同的手机上一切都可能不同。
When to save data to database, onPause() or onStop()?
要么。它们几乎相同,尤其是在 Android 3.0+ 上。
如果正在接管前台的activity是典型的全屏activity,那么前面的activity就看不到了,onPause()
并且onStop()
将快速连续调用。
如果占据前台的 activity 的主题更像是一个对话框,而之前的 activity 仍然可见,则 onPause()
将被调用,但不会onStop()
,直到 activity 不再可见(例如,用户现在按 HOME)。
大多数应用并不担心 "themed to be more like a dialog" 场景,在这种情况下 onPause()
和 onStop()
会一个接一个地调用,您可以将后台线程分叉到将您的数据保存在对您有意义的任何一个中。
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()
也是如此,因为这两个方法都是在主应用程序线程上调用的。
So, is it OK to assume that any Android device (including different ROMS) will ensure a call to onStop on the activity?
从进程终止的角度来看,onPause()
和 onStop()
都具有相同的特征。两者都应该被调用(正常情况),或者两者都不被调用(例如,你崩溃了,电池从 phone 的背面弹出)。
And this is the best place to make any time consuming storage writing of the App?
onPause()
或 onStop()
都是触发工作的好地方,在后台线程上完成,以持久保存您的数据。如果您更喜欢在 onStop()
中完成这项工作,我们绝对欢迎您这样做。就个人而言,我是 onPause()
类型的人。
我知道这个问题是史前的,但像我这样的人似乎仍然碰巧遇到了这个问题。所以我想添加我在文档中找到的答案可能是有意义的,其中指出:
To save persistent data, such as user preferences or data for a database, you should take appropriate opportunities when your activity is in the foreground. If no such opportunity arises, you should save such data during the onStop() method.
等等,还有更多:
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 be killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.
总而言之,在暂停时持久化数据似乎是最好的做法。
我知道这个问题已被问过一百万次,尽管我自己已经知道答案并且正确的答案是唯一有保证的调用是 onPause(),因此您应该将数据保存在那里。
然而,在 android 文档的许多地方,他们总是建议不要在 onPause() 方法中做繁重的工作(例如在数据库中写入数据),因为它会延迟活动之间的转换。
根据Android Developer Guide in Table 1
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.
Killable: YES
然后根据Android Developer Reference Guide in the similar table.
它说的是同样的事情但是:
Killable: Pre-HONEYCOMB
然后他们添加了一个小注释,上面写着:
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.
可杀
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.
FOR POST-HONEYCOMB(我不关心早期版本): 那么,是否可以假设任何 Android 设备(包括不同的 ROM)将确保在 activity 上调用 onStop?而这里是让任何耗时的存储写入App的最佳位置?
注意:这非常令人困惑,因为这里的大多数答案、网站、书籍,甚至在线 android 测试都将正确答案视为正确答案,您应该将其保存在 onPause 而不是 onStop .
- 如果您想要更安全,请存储在
onPause
。 - 如果你的数据太大,需要保存几秒,可以打开后台
Service
(如IntentService
)保存。 - 您还可以在代码中检查系统版本并选择ose 保存时间。
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){}
- 在 most 种情况下,这个何时保存的规则不会被一些习惯 os 改变。但是当然可以有一些其他 os 肯定会改变它。所以在 android 开发中最重要的 os 是你需要知道在不同的手机上一切都可能不同。
When to save data to database, onPause() or onStop()?
要么。它们几乎相同,尤其是在 Android 3.0+ 上。
如果正在接管前台的activity是典型的全屏activity,那么前面的activity就看不到了,onPause()
并且onStop()
将快速连续调用。
如果占据前台的 activity 的主题更像是一个对话框,而之前的 activity 仍然可见,则 onPause()
将被调用,但不会onStop()
,直到 activity 不再可见(例如,用户现在按 HOME)。
大多数应用并不担心 "themed to be more like a dialog" 场景,在这种情况下 onPause()
和 onStop()
会一个接一个地调用,您可以将后台线程分叉到将您的数据保存在对您有意义的任何一个中。
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()
也是如此,因为这两个方法都是在主应用程序线程上调用的。
So, is it OK to assume that any Android device (including different ROMS) will ensure a call to onStop on the activity?
从进程终止的角度来看,onPause()
和 onStop()
都具有相同的特征。两者都应该被调用(正常情况),或者两者都不被调用(例如,你崩溃了,电池从 phone 的背面弹出)。
And this is the best place to make any time consuming storage writing of the App?
onPause()
或 onStop()
都是触发工作的好地方,在后台线程上完成,以持久保存您的数据。如果您更喜欢在 onStop()
中完成这项工作,我们绝对欢迎您这样做。就个人而言,我是 onPause()
类型的人。
我知道这个问题是史前的,但像我这样的人似乎仍然碰巧遇到了这个问题。所以我想添加我在文档中找到的答案可能是有意义的,其中指出:
To save persistent data, such as user preferences or data for a database, you should take appropriate opportunities when your activity is in the foreground. If no such opportunity arises, you should save such data during the onStop() method.
等等,还有更多:
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 be killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.
总而言之,在暂停时持久化数据似乎是最好的做法。