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.
Android - don't refresh Google Maps v2 on rotation
4.5k views Asked by Anonymous1 AtThere are 4 answers
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'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.
I fixed the problem by adding to the Activity in AndroidManifest.xml:
android:configChanges="orientation|screenSize"
Edit (04/28/2017) :
From the Android Documentation:
Maybe you could look at this answer, which properly saves the
SupportMapFragment
using theBundle
available in theonCreate
method ofFragmentActivity
.