Android应用程序在后台运行时保存数据 [英] Android Application saving data when app in background

查看:497
本文介绍了Android应用程序在后台运行时保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在开发xmmp客户端,当应用程序处于前台时,该客户端可以很好地工作.但是由于这样的事实,当应用程序在后台运行时,我会在应用程序类中保留大量数据(复杂的ArrayList,字符串和布尔值作为公共静态变量),每个字段都被垃圾收集了.

currently I'm developing xmmp client which is working quite fine when app is in foreground. But due to the fact that im keeping quite lot of data in Application class (complex ArrayList, Strings and booleans as public statics) when app is in background every field is being garbage-collected.

所以我的问题是,当我从后台获取应用程序时,检索此数据的最佳方法是什么?这是一个很大的问题,因为到目前为止,我在每个类中都使用此公共静态变量,这会导致我的应用崩溃.问题在于用户可以将应用程序的每项活动都置于后台,并且在Application类中重新填充数据似乎有些困难.也许还有另一种方法可以使数据具有全局访问权限,而不会在后台消失?

So my question is what is the best way to retrieve this data when i get app from background? Its quite a problem because I use this public statics in every class so far and it causes my app to crash. The problem is the fact that user can put app in background from every activity and repopulating data in Application class seems like something hard. Maybe there is another way of keeping data with global access to them, that wont be wiped down in background?

预先感谢

推荐答案

如果问题是在backgroud中您的应用程序被android破坏,则标准方法是将数据保存在您的onDestroy()函数中主要活动,并在再次打开to应用程序时(使用捆绑包)在onCreate方法中检索此数据.

If the problem is that your app is destroyed by android when in backgroud, the standard way to do would be to save data in the onDestroy() function of your main activity, and retrieve this data in the onCreate method when to app is opened again ( using the bundle).

如果您希望即使在手机关闭的情况下也能保留数据,则可以考虑使用永久存储选项,例如数据库.看看 http://developer.android.com/guide/topics/data/data-storage.html

If you want the data to persist even when the phone is turned off, you can considere permanent storage options like a database. Have a look at http://developer.android.com/guide/topics/data/data-storage.html

最后,如果您想要一种在后台继续运行的方法,可以看看Service类.但是,如果需要您提供更多的编码,以便能够在您的服务和活动之间进行交流

Finally, if you would like a way to keep running in background, you could have a look at the Service class. But if would demand some more coding from you to be able to communicate between your service and the activity

这篇关于Android应用程序在后台运行时保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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