静态的LinkedHashMap或共享preference? [英] Static Linkedhashmap or Sharedpreference?

查看:220
本文介绍了静态的LinkedHashMap或共享preference?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android应用程序有两个解决方案,活动之间传递数据(没有额外意向请!)

 公共类A {
  公共静态的LinkedHashMap<字符串,字符串> hashStore =新LinkedHasMap<字符串,字符串>();

  公共无效doHttp(){
    //一些HTTP调用和存储一些JSON值
    hashStore.put(数据,jsonKeyValue);
  }

  公共无效的onDestroy(){
    hashStore.remove(键); //删除数据的关键
    hashStore.clear();
  }
  }

公共类B {
公共无效getHttp(){
    //一些HTTP调用
    字符串额外= A.hashStore.get(数据);
  }}


    //共享preference电话
公共类A {
共享preference散列preF; //宣言的onCreate
公共无效dohttp(){
//某些HTTP和共享preferences商店值
哈希pref.put(数据,jsonkeyvalue);
哈希pref.apply();
}}



 公共类B {
 共享preference散列preF; //宣言的onCreate
 公共无效getHttp(){
    //一些HTTP调用
    字符串额外=散列pref.get(数据);
  }}
 

  1. 在哪一个是更好的选择,以减少内存泄漏?
  2. 如果我存储超过30〜40项其中之一将是preferrable?
  3. 如果我使用的共享preferences将不我吃的性能,同时验证并更新键??
  4. 是否有任何替代方案,我可以使用这两种解决方案呢? (别提意向额外这里。)
解决方案

使用你的活动作为一个静态的数据源,并让你的其他活动依赖于它(甚至知道这个活动存在)是一种不好的做法,根据许多原则(高内聚,去耦,minimice的依赖,保护变量... ),但Android团队有可以依靠一些类似的方法,但由于同样的原因,他们不应该是第一个选项。

我会preFER是:

如果数据必须是持久的:

  • 共享preferences的数据量小
  • 在SQLite数据库进行数据的较大金额

如果你不需要persit数据

  • 系统服务,始终运行,并绑定到所有的活动,那就是你有所有常用数据的功能。
  • ,你必须该数据的辅助对象。

您可以找到更多的这款Android官方常见问题解答页面或在这个伟大的 SO回答,是基于在回答问题,但与一些code例子

Android App with two solution for passing data between activities (No Intent Extras Please!)

public class A { 
  public static LinkedHashMap<String,String> hashStore = new LinkedHasMap<String,String>(); 

  public void doHttp(){
    //Some HTTP call and store some json value
    hashStore.put("data","jsonKeyValue"); 
  }

  public void onDestroy(){
    hashStore.remove(key);// remove data key
    hashStore.clear();
  }
  }

public class B { 
public void getHttp(){
    //Some HTTP call 
    String extra = A.hashStore.get("data"); 
  }}


    //  SharedPreference Call 
public class A{
SharedPreference hashPref ; //declaration on onCreate 
public void dohttp(){
//Some Http and Store value in SharedPreferences
hashPref.put("data","jsonkeyvalue");
hashPref.apply();
}}



 public class B{
 SharedPreference hashPref ; //declaration on onCreate
 public void getHttp(){ 
    //Some HTTP call 
    String extra = hashPref.get("data"); 
  }}

  1. Which one is better option to reduce memory leaks ?
  2. If i store more than 30-40 keys which one would be preferrable ?
  3. If i use sharedpreferences won't i eat the performance while validating and updating the keys ??
  4. Is there any alternative that i could use instead of these two solutions ? (Don't mention Intent Extras here.)

解决方案

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

这篇关于静态的LinkedHashMap或共享preference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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