title: Android Fragment, static variables not initialized on "new"?
body: <p>I keep getting some old Data in a Fragment that I basically want to have built itself up from Scratch.
  It's driving me nuts ;)</p>
  
  <p>Here are the relevant code bits:  In activity, when Fragment is created:</p>
  
  <pre><code>        ...
          Log.w(LOG_TAG, "remoteFrag is NOT available");
  
          // Create fragment
          int savePos=mDataManager.getPosition();
          mDataManager.setPosition(position);
          Bundle args = new Bundle();
          remoteFrag = new RemoteFragment();
          args.putSerializable(ExtraXMLParser, mDataManager);
          Log.w(LOG_TAG, "mDataManager is " + ((mDataManager == null)? "not ":"") + "set");
          remoteFrag.setArguments(args);
          FragmentTransaction transaction = getFragmentManager().beginTransaction();
  
          transaction.replace(R.id.fragment_container, remoteFrag);
          if(mDataManager.getPosition()!=savePos)
              transaction.addToBackStack(null);
  
          // Commit the transaction
          transaction.commit();
          ...
  </code></pre>
  
  <p>What do I want to achieve: In the activity, that I want to relaunch (basically, the App behaves different if I am in a local Wifi network, Menu is different and different Options are available.  A splash screen probes for this type of environment and initializes the DataManager appropriately).</p>
  
  <pre><code>        ...
          Log.w(LOG_TAG,"terminating...");
          leaveRemoteView();
          FragmentTransaction transaction = getFragmentManager().beginTransaction();
          transaction.remove(remoteFrag);
          remoteFrag=null;
          mDataManager=null;
          Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName() );
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          finish();
          ...
  </code></pre>
  
  <p>In the remoteFrag I have:</p>
  
  <pre><code>public class RemoteFragment extends Fragment implements DataManager.ProcessXMLData  {
  RemoteDetail remoteBottom = null;
  private View thisView;
  private ViewGroup thisContainer;
  static private DataManager _dataManager = null;
  static private boolean _dataManagerRegistered = false;
  
  final private static String LOG_TAG = "RemoteFragment";
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) {
  
      // If activity recreated (such as from screen rotate), restore
      // the previous article selection set by onSaveInstanceState().
      // This is primarily necessary when in the two-pane layout.
      Log.w(LOG_TAG, "creating RemoteFragment");
      if(_dataManager == null) {
          try {
              _dataManager = (DataManager) getArguments().getSerializable(RemoteDisplay.ExtraXMLParser);
          }
          catch (NullPointerException e) {
              _dataManager = null;
              Log.e(LOG_TAG , "mXmlParser not passed as arguments");
          }
      }
      else
          Log.d(LOG_TAG, "dataManager was not null");
      thisContainer = container;
      ...
  </code></pre>
  
  <p>If I force the app to restart with the second bit of code above, and I make the Fragment launch with the first and third section, I receive the following LogCat:</p>
  
  <pre><code>03-21 14:32:37.930: D/ViewRootImpl(12915): ViewPostImeInputStage ACTION_DOWN
  03-21 14:32:38.090: D/RemoteDisplay(12915): selection is 0
  03-21 14:32:38.090: W/RemoteDisplay(12915): remoteFrag is NOT available
  03-21 14:32:38.090: W/RemoteDisplay(12915): mDataManager is set
  03-21 14:32:38.110: W/RemoteFragment(12915): creating RemoteFragment
  03-21 14:32:38.110: D/RemoteFragment(12915): dataManager was not null
  03-21 14:32:38.110: D/RemoteFragment(12915): will inflate now...
  03-21 14:32:38.130: D/RemoteFragment(12915): finshed create view
  </code></pre>
  
  <p>The confusing message that messes everything up is "dataManager was not null".
  This means the fragment did not try to receive the variable _dataManager from the activity passed as argument, and the initialization on the top of the class is overwritten somewhere before OnCreateView is called with an old value.</p>
  
  <p>Where and what can I do to avoid this?  The only way I managed to behave the fragment as I want is to completely remove the App from the recent activities.</p>
  
  <p>Sorry if this turned out to be somewhat long... ;)</p>