在WeakReferences的HashMap的活动之间传递数据 [英] HashMap of WeakReferences for passing data between activities

查看:112
本文介绍了在WeakReferences的HashMap的活动之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从官方Android常见问题

  

在WeakReferences的一个HashMap到对象

     

您也可以使用在WeakReferences的一个HashMap与长键的对象。当一个活动想将对象传递到另一个活动,它简单地把该对象中的地图,并发送经由意图额外的键(这是一个独特的龙基于计数器或时间戳)给收件人活性。收件人活动获取使用此key.`对象

我还没有找到一种方法,如何正确地实现这一点。而且我不知道为什么在WeakReferences是preferred这里为什么不使用硬引用。

我的实现(我想送的一个实例的类XY 活动A 服务B ):

  • 接收服务有对象的静态HashMap中。

     公共静态的HashMap<长,对象>参数=新的HashMap<长,对象>();
     

  • code的发送部分(活动A)

     长键= SystemClock.elapsedRealtime();
    B.parameters.put(重点,新的XY());
    意图I =新的意图(这一点,B.class);
    i.putExtra(PARAM_UPLOAD键);
    startService(ⅰ);
     

  • code的接收部分(服务B)

     长键= intent.getLongExtra(PARAM_UPLOAD,-1);
    XY数据=(XY)parameters.get(密钥);
     

在code使用硬引用。为什么我要在这里使用弱引用(所建议的常见问题)?而就是这样一个使用模式传递数据确定或你preFER别的东西。

解决方案
  

为什么要我在这里使用弱引用(所建议的常见问题)?

由于您正在泄漏内存。任何你在那静放的HashMap 是从来没有垃圾收集。

  

和这样的使用模式传递数据确定或你preFER别的东西。

我preFER只有活动之间传递简单的数据。凡是不是原始(或系统提供的 Parcelable ,像 PendingIntent )应考虑部分数据模型,并应这样进行管理。 这里是一个博客帖子我走到哪里,更多的细节。

I am particulary interested in the following suggestion from the official android FAQ.

A HashMap of WeakReferences to Objects

You can also use a HashMap of WeakReferences to Objects with Long keys. When an activity wants to pass an object to another activity, it simply puts the object in the map and sends the key (which is a unique Long based on a counter or time stamp) to the recipient activity via intent extras. The recipient activity retrieves the object using this key.`

I haven't found a way how to properly implement this. And I'm not sure why WeakReferences are preferred here and why not use hard references.

My implementation (I would like to send an instance of class XY from the activity A to the service B):

  • the receiving service has a static HashMap of Objects.

    public static HashMap<Long, Object> parameters = new HashMap<Long, Object>();
    

  • code for the sending part (activity A)

    long key = SystemClock.elapsedRealtime();
    B.parameters.put(key, new XY());
    Intent i = new Intent(this, B.class);
    i.putExtra("PARAM_UPLOAD", key);
    startService(i);
    

  • code for the receiving part (service B)

    long key = intent.getLongExtra("PARAM_UPLOAD", -1);
    XY data = (XY)parameters.get(key);
    

The code is using hard references. Why should I use weak references here (as proposed by the FAQ)? And is such a use pattern for passing data ok or do you prefer something else.

解决方案

Why should I use weak references here (as proposed by the FAQ)?

Because you are leaking memory. Anything you put in that static HashMap is never garbage-collected.

And is such a use pattern for passing data ok or do you prefer something else.

I prefer only passing simple data between activities. Anything that isn't a primitive (or a system-supplied Parcelable, like a PendingIntent) should be considered part of the data model and should be managed as such. Here is a blog post where I go into more details.

这篇关于在WeakReferences的HashMap的活动之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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