Android - don't refresh Google Maps v2 on rotation

4.5k views Asked by At

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.

4

There are 4 answers

0
Anonymous1 On BEST ANSWER

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.

8
Emil Adz On

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.

5
fweigl On

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.

0
Aaron Fleshner On

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.