title: Google Maps v2 sometimes resets when resuming app
body: <p>I am using Google Maps v2 in my Android application. Everything works fine, but if I leave the application (using the home button), use the device for other stuff and then return to my app, the maps just reset themselves and discard all my settings and listeners.</p>
  
  <p>I think it's because my phone doesn't have that much RAM so when it needs it for running other apps, something that's vital for my app gets lost... If I don't use other apps in the meantime, it resumes with all the settings and listeners intact.</p>
  
  <p>So I have a member variable <code>private GoogleMap map = null;</code></p>
  
  <p>I need to create the map programatically because I am putting it into a <code>LinearLayout</code> (<code>fragment_wrapper</code>) that I also want to use for other fragments. So I am doing this in my <code>onCreate()</code> method:</p>
  
  <pre><code>mapFragment = MapFragment.newInstance();
  FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  fragmentTransaction.add(R.id.fragment_wrapper, mapFragment);
  fragmentTransaction.commit();
  </code></pre>
  
  <p>The map doesn't get initialized very quickly, so I need to set its settings and listeners in the <code>onResume()</code> method:</p>
  
  <pre><code>if (map == null) {  
      map = mapFragment.getMap();
      UiSettings settings = map.getUiSettings();
      settings.setMyLocationButtonEnabled(false);
      settings.setZoomControlsEnabled(false);
      (...)
      map.setOnMapClickListener((...));
      (...)
  }
  </code></pre>
  
  <p>I originally put the <em>if</em> block there so that the app wouldn't have to set the map's settings and listeners again on resuming. But then I noticed it forgets the settings. So I thought that if I deleted the <em>if</em> block, it would just set up the map again since <code>onResume()</code> gets called when the app is resumed. But it doesn't work either... So all the map's settings and listeners are gone which renders the map useless and my app needs to be restarted in order to set up the map again.</p>
  
  <p>Can anyone please tell me what the problem could be and how to solve it? Thank you.</p>