Static Linkedhashmap or Sharedpreference?

偶尔善良 提交于 2019-12-04 17:35:43
Carlos Robles

Using your activity as an static data source, and let your other activities depend on it (and even know that this activity exists) is a bad practice, according to many principles (high cohesion, decoupling, minimice dependency, Protected Variations...), but android team has some similar approaches that you can rely on, but for the same reason they shouldnt be the first option.

What i would prefer is:

If the data has to be persistent:

  • SharedPreferences for small amount of data
  • SQLite database for a bigger amount of data

if you dont need to persit the data

  • A service that is always running and you bind to all your activities, where you have all common data an functions.
  • A helper object where you have the data.

You can find out more in this android official faq page or in this great SO answer, that is based in that faq but with some code examples

axierjhtjz

As stated here,

Pros and Cons of SQLite and Shared Preferences

for large groups of data > 30-40 keys I would suggest you to use SQLite. It´s more powerful than SharedPreferences and provides you the possibility to make queries based on the keys you want to search for.

In the other hand implementing SQLite it´s a little bit more complicated and longer to code than using SharedPreferences.

Having a static LinkedHashMap or any other static variable it´s never a good option for code maintenance and readability, you should use it only if there´s no other option.

Hope it helps. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!