Reboot V2 SupportMapFragment Android Cards while spinning

I want to improve the performance of SupportMapFragment when the device is rotated. It seems that the fragment should be recreated. I am not sure about this, however, when the device is rotated, the map tiles must be rebooted. It would be prudent in terms of performance to have the entire wireframe fragment stored and reused without the need for a re-instance of the fragment. Any understanding of this would be appreciated.

I declare SupportMapFragment in xml and using SetupMapIfNeeded () as described in api docs.

private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the // map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } 
+7
source share
1 answer

Check out the RetainMapActivity class from the samples. It works like a charm. That's what:

 public class RetainMapActivity extends FragmentActivity { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.basic_demo); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); if (savedInstanceState == null) { // First incarnation of this activity. mapFragment.setRetainInstance(true); } else { // Reincarnated activity. The obtained map is the same map instance in the previous // activity life cycle. There is no need to reinitialize it. mMap = mapFragment.getMap(); } setUpMapIfNeeded(); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { if (mMap == null) { mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); } 

}

+10
source

All Articles