android应用程序/活动生命周期:我们什么时候应该保存到Flash?
我有一个由许多活动组成的应用程序。每个活动都可以以某种方式修改项目。我通过序列化一些对象并将它们保存到闪存来将该项目保存到闪存。 现在,如果用户离开应用程序,它可能会被操作系统杀死。 因此,我应该在每个活动的每个 onPause() 处保存..
但这非常愚蠢且耗时(序列化、打开文件、覆盖文件..)..
你有更好的主意吗?顺便说一句,如果我不想使用数据库,保存我的项目是对象序列化足够好还是我应该使用更强大的东西(xml?)?
感谢您的澄清和建议..
I have an app that is composed of many activities. Each activity can modify the project in some way. I'm saving this project to flash by serializing a few objects and saving them to flash.
Now, If the user leaves the app, it's possible that it gets killed by the os.
Therefore, I should save at every onPause() on every activity..
But this is very stupid and consuming (serialize, open file, overwrite file..)..
Do you have any better idea? By the way, if I don't want to use a Database, to save my projects is object serialization good enough or should I use something more robust (xml?) ?
Thanks for the clarifications and suggestions..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在每次 onPause 时保存并在每次 onResume 时恢复。如果堆栈上有多个相同的活动,这可以防止出现一些奇怪的情况。
大多数 Google 应用程序也会执行此操作,或者如果您想提示正常保存,则保存到中间位置。
如果它不会导致性能问题并且满足您对可维护性的要求,那么它可能就足够了。
I save at every onPause and restore in every onResume. This prevents some oddities if you have multiples of the same activity on the stack.
Most Google apps do this as well, or save to an intermediate location if you want to prompt for saving normally.
If it's not causing performance issues and meets your requirements for maintainability then it's probably good enough.
您可以实现
Application
类并使用 这个回调You could implement
Application
class and use this callback