Question

When I rotate my device, Google Maps v2 refreshes. How do I prevent this refresh from occurring? I'm adding the map dynamically in a tab of a SherlockFragment.

Était-ce utile?

La solution

I fixed the problem by adding to the Activity in AndroidManifest.xml:

android:configChanges="orientation|screenSize"

Edit (04/28/2017) :

From the Android Documentation:

Note: Using this attribute should be avoided and used only as a last resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

Maybe you could look at this answer, which properly saves the SupportMapFragment using the Bundle available in the onCreate method of FragmentActivity.

Autres conseils

Emil's comment is right in explaining why the view of your map is being reset. Forbidding the orientation change via the manifest however is not good style.

You need to retain the map fragment somehow or retain the fragment that contains the map fragment. After your 'host' activity has been recreated, you need to reattach the fragment to the activity instead of creating a new one. I don't have any code here but you'll find information about retaining fragments on the web.

As an alternative you could save your maps state (foremost the camera position I guess) somewhere else and restore it after the fragment has been recreated.

You can't, the map is getting refreshed because the Activity is getting re-created on configuration changes (screen rotations...). You could prevent the rotation of the Activity by using:

android:screenOrientation="landscape"

or

android:screenOrientation="portrait"

in the Manifest file under your map Activity.

and this way prevent it from being recreated on rotations.

Sadly the way Google Handles map refreshing issue that you are seeing by

android:configChanges="orientation|uiMode|screenSize|fontScale"  
android:screenOrientation="user" 

on the Activity tag in the Manifest in the Google maps application.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top