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.