Android - don't refresh Google Maps v2 on rota

2019-02-17 02:57发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-02-17 03:00

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.

查看更多
Viruses.
3楼-- · 2019-02-17 03:08

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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-17 03:13

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.

查看更多
We Are One
5楼-- · 2019-02-17 03:21

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.

查看更多
登录 后发表回答