title: HashMap of WeakReferences for passing data between activities
body: <p>I am particulary interested in the following suggestion from the <a href="http://developer.android.com/guide/appendix/faq/framework.html" rel="nofollow">official android FAQ</a>.</p>
  
  <blockquote>
    <p><strong>A HashMap of WeakReferences to Objects</strong></p>
    
    <p>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.`</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>My implementation (I would like to send an instance of <strong>class XY</strong> from the <strong>activity A</strong> to the <strong>service B</strong>):</p>
  
  <ul>
  <li><p>the receiving service has a static HashMap of Objects.</p>
  
  <pre><code>public static HashMap&lt;Long, Object&gt; parameters = new HashMap&lt;Long, Object&gt;();
  </code></pre></li>
  <li><p>code for the sending part (activity A)</p>
  
  <pre><code>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></pre></li>
  <li><p>code for the receiving part (service B)</p>
  
  <pre><code>long key = intent.getLongExtra("PARAM_UPLOAD", -1);
  XY data = (XY)parameters.get(key);
  </code></pre></li>
  </ul>
  
  <p>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. </p>