title: Static pattern to retain objects through configuration changes
body: <p><em>I am experienced enough to know about configuration changes. I know how to save and restore the instance state of a <code>View</code>, <code>Activity</code>, <code>Fragment</code> or whatever, and I know that we should rely on that, rather than simply make objects static.</em></p>
  
  <p>This question is about <code>static</code>ness of a field. In "pure Java" declaring an object as <code>static</code> serves the purpose of having it shared among instances.
  In the android system, declaring a <code>static</code> field is also a quick way to have it <em>survive</em> configuration changes, when our instance is recreated and its fields are cleared.</p>
  
  <p>Example:</p>
  
  <pre><code>public class FooFragment extends Fragment {
  
      private static Object sObject;
  
      public void onViewCreated(View view, Bundle savedInstanceState) {
          if (savedInstanceState == null) {
              sObject = new Object();
              sObject.toString();
          } else {
              sObject.toString();
          }
      }
  
  }
  </code></pre>
  
  <p>In this case, <code>sObject</code> can be initialized when the fragment is <strong>created</strong>, and just fetched when the fragment is <strong>recreated</strong>. Issues arise when our class is not static itself, is designed as a base class, or is an <code>abstract</code> class.</p>
  
  <pre><code>public abstract class BaseFragment extends Fragment {
  
      protected static Object sObject;
  }
  
  public class FooFragment extends BaseFragment {
  
      public void onViewCreated(View view, Bundle savedInstanceState) {
          if (savedInstanceState == null) {
              sObject = new Object();
              sObject.toString();
          } else {
              sObject.toString();
          }
      }
  }
  
  public class BarFragment extends BaseFragment {
  
      public void onViewCreated(View view, Bundle savedInstanceState) {
          if (savedInstanceState == null) {
              sObject = new Object();
              sObject.toString();
          } else {
              sObject.toString();
          }
      }
  }
  </code></pre>
  
  <p>Now both <code>FooFragment</code>  and <code>BarFragment</code> will access (and impredictably use) the same instance of <code>sObject</code>. That is the point of staticness, but is not what we want. We want <code>FooFragment</code> and <code>BarFragment</code> to have <strong>their own sObject</strong>; we just want it to survive configuration changes and platform-driven recreation.</p>
  
  <h1>Question</h1>
  
  <p>How can I design the base class for this goal?</p>
  
  <p>This is what I came up with, but I’m sure there’s something better.</p>
  
  <pre><code>public abstract class BaseFragment extends Fragment {
  
      private static HashMap&lt;String, Object&gt; sObjects;
  
      public void onViewCreated(View view, Bundle savedInstanceState) {
          if (savedInstanceState == null) {
              Object o = new Object();
              sObjects.put(getClass().getName(), o);
          }
      }
  
      protected final Object getObject() {
          return sObjects.get(getClass().getName());
      }
  }
  </code></pre>
  
  <p>Basically I keep a static map, indexing by child class name.</p>