0

I'm displaying a GoogleMap in my Fragment. When I rotate the screen the map gets reloaded. In the GoogleMaps app the map does not get reloaded(Map disappears, grey screen and appears again)

My code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_map, container,
            false);

    mMapView = (MapView) v.findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);
    mMapView.onResume();

    if(savedInstanceState == null){
        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    if(googleMap == null){
        googleMap = mMapView.getMap();

        //map settings
        googleMap.setMyLocationEnabled(true);
        googleMap.setIndoorEnabled(false);
        googleMap.setTrafficEnabled(false);
        googleMap.getUiSettings().setTiltGesturesEnabled(false);
        googleMap.getUiSettings().setMapToolbarEnabled(false);
    }

    if(savedInstanceState == null) {
        Location myLocation = getMyLocation();

        if (myLocation != null) {
            double myLatitude = myLocation.getLatitude();
            double myLongitude = myLocation.getLongitude();

            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(myLatitude, myLongitude)).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    }
    return v;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mMapView.onSaveInstanceState(outState);
}

// and ofcouse onResume, OnPause, OnCreated and onLowMemory

My camera postion is retained, but the map is not. It is reloaded which takes time and looks ugly. How can I prevent my map from reloading like the google maps app?

I took a look at this question, but I don't want to forbid orientation change.

Community
  • 1
  • 1
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116

2 Answers2

1

Have you tried adding

android:configChanges="orientation|screenSize"

to the activity in your manifest?

It doesn't necessarily lock orientation, it allows android to try and deal with configuration changes by itself.

If this isn't what you're looking for, you could try and call

setRetainInstanceState(true)

after initializing your fragment, and recall the data when resumed after orientation, however, it's likely that because an orientation change forces android to destroy and re-create your activity, GoogleMaps will always have to reload.

Harry
  • 197
  • 3
  • 16
0

I will advice you set the screen Orientation to portrait in your map activity in the Manifest file. See the below example.

        <activity
        android:name=".activity.MapActivity"
        android:label="@string/title_activity_map"
        android:theme="@style/AppTheme"
        android:screenOrientation="portrait"/>

This worked for me

Gino Wine
  • 1
  • 2