用于在活动之间传递数据的 WeakReferences 的 HashMap

发布于 2024-10-02 00:58:23 字数 1066 浏览 1 评论 0原文

我对官方 Android 常见问题解答中的以下建议特别感兴趣。

对象弱引用的哈希映射

您还可以使用对具有长键的对象的弱引用的 HashMap。当一个活动想要将对象传递给另一个活动时,它只需将该对象放入映射中,并通过 Intent extras 将密钥(这是基于计数器或时间戳的唯一 Long)发送到接收活动。接收者活动使用此密钥检索对象。`

我还没有找到如何正确实现此功能的方法。我不确定为什么这里首选弱引用以及为什么不使用硬引用。

我的实现(我想将类 XY 的实例从活动 A 发送到服务 B):

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

    public static HashMap;参数 = new HashMap();
    
  • 发送部分的代码(活动 A)

    长键 = SystemClock.elapsedRealtime();
    B.parameters.put(key, new XY());
    意图 i = new Intent(this, B.class);
    i.putExtra("PARAM_UPLOAD", key);
    启动服务(i);
    
  • 接收部分(服务 B)的代码

    long key = Intent.getLongExtra("PARAM_UPLOAD", -1);
    XY 数据 = (XY)parameters.get(key);
    

该代码使用硬引用。为什么我应该在这里使用弱引用(如常见问题解答所建议的)?这种传递数据的使用模式是否可以,或者您更喜欢其他的方式。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-10-09 00:58:23

为什么我应该在这里使用弱引用(如常见问题解答所建议的)?

因为你正在泄漏内存。您放入静态 HashMap 中的任何内容都不会被垃圾收集。

这种传递数据的使用模式是否可以,或者您更喜欢其他方式。

我更喜欢只在活动之间传递简单的数据。任何不是原语(或系统提供的Parcelable,如PendingIntent)的东西都应该被视为数据模型的一部分,并且应该这样进行管理。 这是一篇博客文章,我将在其中详细介绍。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文