7

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.

Anonymous1
  • 3,877
  • 3
  • 28
  • 42

4 Answers4

16

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.

Community
  • 1
  • 1
Anonymous1
  • 3,877
  • 3
  • 28
  • 42
2

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.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • There is nothing bad in forbidding the orientation change via the manifest if it's suites the application needs. – Emil Adz Dec 15 '13 at 01:03
  • Sure, but in this case it seems like a lazy workaround for a problem that could be solved easily in a way more elegant fashion. What if we have markers on the map with info windows? They will be displayed sideways. Also the ActionBar will be sideways. There's a reason why Google does not forbid the orientation change in their maps app. – fweigl Dec 15 '13 at 01:06
  • Yes they will. but some time you don't want to show the map in landscape – Emil Adz Dec 15 '13 at 01:07
  • It does not suit this application's needs. I need both orientations. – Anonymous1 Dec 15 '13 at 01:08
  • I think it's up to the user to decide which orientation is better for him to view the map on. – fweigl Dec 15 '13 at 01:08
0

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.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • Maps by Google doesn't have this issue. Are they circumventing it with code that developers cannot access? – Anonymous1 Dec 15 '13 at 00:45
  • The google map application doesn't get rotated on device configuration change. They are doing exactly what I have described in the answer. – Emil Adz Dec 15 '13 at 00:46
  • @Ascorbin, I guess you could add more then that? – Emil Adz Dec 15 '13 at 00:54
  • When I added this code to the Activity, the app was forced into that orientation. Maps by Google does not force an orientation. – Anonymous1 Dec 15 '13 at 01:03
  • yeah you right, you could do that by preserving the state of the map fragment and recreating the fragment on rotation with the preserved data. – Emil Adz Dec 15 '13 at 01:08
  • The map would still refresh, which I don't want. – Anonymous1 Dec 15 '13 at 01:09
  • But the map is getting refreshed in Google Maps application as well, all the Activity is. – Emil Adz Dec 15 '13 at 01:10
  • 1
    When I change the orientation of my device, Google Maps does not show a "gray box" pattern. My app shows this "gray box" pattern before filling in the map. – Anonymous1 Dec 15 '13 at 01:18
0

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.

Aaron Fleshner
  • 659
  • 3
  • 9