Android:アプリケーションシングルトンクラスに保存されているデータを保存するための最良の方法

StackOverflow https://stackoverflow.com/questions/6063550

  •  16-11-2019
  •  | 
  •  

質問

Androidアプリケーションのアプリケーションクラス(シングルトン)に保存されているデータを保存する最善の方法は?

私は活動の間の多くのデータを共有する静かな大きなアプリを持っています。そのため、そのほとんどはアプリケーションシングルトンに保存されています。

それはすべてうまくいっています。utilアプリケーションは低メモリ上のOSによって殺されます...そしてそれが戻ってきたとき、それはアプリケーションであった必要なデータがないために成功することなく活動を再開しようとします。< / P>

あなたの経験に従ってアプリケーション上のデータを保存するための大理解(そして必要な)方法がないため、最良のアプローチは何ですか?

ビットマップのように、「通常の」文字列、ブール値などのほかにものを保存できますか?

私はすでにこれを見ました Androidのグローバル変数を宣言する方法?しかし、この場合、メモリが低いためにアプリケーションが殺されたときにデータを保存する方法はどのように重要なのかをどのように保存するかに焦点を当てていません...

役に立ちましたか?

解決

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
    1. A blob column in a database
    2. 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.

他のヒント

It's important to note that if you are using a singleton class to hold your information and your application is forced to stop, the information will be cleared.

For shared preferences the information will remain the same.

Hope this helps.

There is Java serializer, not sure is that what you need.

I personally use GSON for all of that. It's google library to work with JSON. It allows to serialize objects into efficient string representation.

I used this mainly for RESTful service communication but then learned that it works very good to store object representation to SQLLite or whatever. I can inflate object very easy this way.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top