安卓:保存存储在应用单例类数据的最佳方式方式、数据、单例类

2023-09-06 00:46:45 作者:伴我走还是掉头走

什么是节省存储在一个Android应用程序的应用类(单)数据的最佳方式是什么?

我有一个安静的大的应用程序,共享活动之间有很多的数据。所以大部分存储在应用单。

这一切的伟大工程.. UTIL申请是由低内存的操作系统杀死......然后当它回来它试图恢复没有成功的活动,由于缺乏必要的数据,那是以前的应用。

由于缺乏一个更AP $ P $的pciated(和需要)的方法来保存应用程序数据,根据你的经验,什么是最好的办法是什么?

我能保存的东西,除了正常的字符串,布尔值,等等,像位图?

我已经看到了这一点安卓:?如何声明全局变量但问题是不是专注于什么是重要的,这种情况下,如何保存应用程序时,由于杀低内存中的数据...

解决方案

由于有许多问题,没有简单的答案。有许多方式来保存数据,每个都有优点和缺点。 最好的方法将取决于您的特定需求。你把所有的选项这里:http://developer.android.com/guide/topics/data/data-storage.html

对于少数,小位图,你可能会连接code并将其存储在Shared$p$pferences. 有关更多更大的图片,你有两种选择 在database 在它们存储在您的internal存储,并保持在preferences的链接。

共享preferences 商店字符串,所以任何东西是一个字符串,可以存储,包括任何系列化/ EN codeD对象。据this交 ,没有硬codeD大小限制的共享preferences一个序列化的字符串,而是基于字符串大小限制。尽管如此,this其他职位 指出,整个共享preferences对象写入为一个XML文件,所以你应该尽量保持它的大小到最低限度。

JSON对象(或使用GSON所建议的katit)是一个不错的轻量级的选择,但我会采取的方法是将它们保存到内部数据存储(除非数据非常大,也就是说,许多兆字节,和你$ PFER外部存储p $),只保留在共享preferences的链接。我不知道你的对象的样子,但如果他们可以减少到一堆简单的组件,你可以考虑为他们的数据库(即每个对象一行,每场一列,其中包括可能的几个斑点)

在文件VS数据库办法也将取决于有多少次,你打算访问这些对象。如果他们将被读取一次或两次只,然后消失,那么我会选择文件在数据库及其光标的麻烦。我会选择一个数据库是否会有很多读,或许你需要使用查询更快的搜索。

还要检查这个帖子:http://android-developers.blogspot.in/2009/02/faster-screen-orientation-change.html对于活动的具体选项。

What's the best way to save the data stored in the Application Class (singleton) of an Android Application?

I have a quiet big app that shares a lot data between the activities. So most of it is stored on the Application Singleton.

存储空间清理app下载

It all works great.. util the application is killed by the OS on low memory... then when it comes back it tries to resume the activity without success due to the lack of necessary data that was before on Application.

Due to the lack of a much appreciated (and needed) method to save data on Application according to your experience what are the best approaches?

Can i save stuff, besides the "normal" strings, booleans, etc, like Bitmaps?

I have already seen this Android: How to declare global variables? but the question isn't focusing on what is important in this case, how to save the data when the application is killed due to low memory...

解决方案

As with many questions, there is no simple answer. There are many ways to save data and each has advantages and disadvantages. The "best" approach will depend on your particular needs. You have all your options here: http://developer.android.com/guide/topics/data/data-storage.html

For a few, small bitmaps, you might encode them and store them in the SharedPreferences. For more and bigger images, you have two options

A blob column in a database Store them as files in your internal storage, and keep the links in your preferences.

SharedPreferences stores strings, so anything that is a string can be stored, including any serialized/encoded object. According to this post, there is no hardcoded size limit for a serialized string in SharedPreferences, but is based on the String size limit. Nevertheless, this other post points out that the whole SharedPreferences object is written as a single xml file, so you should try to keep its size to a minimum.

JSON object (or using GSON as suggested by katit) are a good lightweight option, but the approach I would take is to save them to the internal data storage (unless the data is really big, i.e., many megabytes, and you prefer the external storage) and keep the links only in the SharedPreferences. I don't know what your objects look like, but if they can be reduced to a bunch of simpler components, you can consider a database for them (i.e., one row per object, one column per field, including perhaps a few blobs).

The files vs database approach would depend also on how many times are you planning to access those objects. If they will be read one or two times only and then disappear, then I would choose files over the hassle of the database and its cursors. I would choose a db if there will be many reads, and perhaps you need a faster search using queries.

Check also this post: http://android-developers.blogspot.in/2009/02/faster-screen-orientation-change.html for an Activity-specific option.