HashMap of WeakReferences for passing data between activities

雨燕双飞 提交于 2019-12-06 11:23:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4154744/hashmap-of-weakreferences-for-passing-data-between-activities

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