Android : 응용 프로그램에 저장된 데이터를 저장하는 가장 좋은 방법 Singleton 클래스

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