Good strategy to pass complicated data structure to fragment or activity
<p>Currently, to pass complicated data structure to a fragment or an activity, I'm using the strategy described by <a href="http://developer.android.com/guide/faq/framework.html#3" rel="nofollow">http://developer.android.com/guide/faq/framework.html#3</a></p>

<blockquote>
  <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 use <code>Application</code> to hold the global accessible <code>HashMap</code>, and use the following code to send across Long key.</p>

<pre><code>public class MyFragment extends Fragment {

    public static MyFragment newInstance(long myArrayId) {
        // This is how I receive the object Id.
        MyFragment myFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(MyApplication.INTENT_EXTRA_MY_ARRAY_ID, myArrayId);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This is how I turn Id into Object.
        this.myArrayIdId = this.getArguments().getLong(MyApplication.INTENT_EXTRA_MY_ARRAY_ID);
        this.myArray = (MyArray)MyApplication.instance().getExtra(myArrayIdId );
</code></pre>

<p>I first thought this is a pretty good strategy. Especially for those complicated data structure, which seems impossible, to implement using <code>Parcelable</code>.</p>

<p>This strategy works pretty well <strong>most of the time</strong>. However, there is situation where it doesn't work. I just find out that, it is possible for the system to destroy <code>Application</code>, if it isn't activated for some time.</p>

<p>This is how to reproduce the problem.</p>

<ol>
<li>Run your app</li>
<li>Press Home</li>
<li>Run other memory intensive app by running them, pressing Home, running the others again.</li>
<li>Long press on Home button. Your app should still appear in the list.</li>
<li>Select it. You will realize the object <code>HashMap</code> is empty. (Following by application crashes, as we get null object)</li>
</ol>

<p>I know one of the reliable solution is avoid storing the object in static <code>HashMap</code>, but putting them in Bundle as <code>Parcelable</code>.</p>

<p>But, if for case it is not possible to put the class as <code>Parcelable</code>, as they are having file handle, thread handle, concurrent data structures, ... what is the strategy to overcome this?</p>

<p>I know quite a number of similar questions had been raised. But seems there is no good solution given. (See, even Google's own FAQ forget to mention storing in static HashMap doesn't work all the time)</p>
