Saving object reference in onSaveInstanceState using Serializable?
<p>I need clarification on how I can save the state of an object that has a reference to another object?</p>

<p>Lets say I have the below class objects that I need to save and restore:</p>

<pre><code>public class ObjectA implements Serializable{
    private List&lt;ObjectB&gt; mObjectBList;
}

public clas ObjectB implements Serializable {
    // some other members here
    private ObjectA mParent;
}
</code></pre>

<p>Here are the code to invoke the save and restore in a fragment:</p>

<pre><code>@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("ObjectA", mObjectA);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        mObjectA = (mObjectA) savedInstanceState.getSerializable("ObjectA");
    }
}
</code></pre>

<p><strong>Questions:</strong></p>

<ol>
<li>Will saving mObjectA also save every object in mObjectBList?</li>
<li>Since mObjectB has a reference to its parent ObjectA, will ObjectA be re-instantiated for each ObjectB? Which then in turn will re-instantiate ObjectB, and then it will re-instantiate ObjectA, and so forth, leading to an infinite loop?</li>
<li>How would you solve this problem?</li>
</ol>

<p>I'm not clear on what happens when an object gets saved as a Serializable, so please help me understand how Android distinguishes when to instantiate a new object and when it will reproduce an actual reference.</p>