I am using the Google Maps API in my Android app, and upon orientation change, the map redraws itself every time since the Activity is destroyed. How does the official Google Maps app prevent this from happening in their app? The map just simply shifts orientation without any jarring disappearing of the map. Are they using a complicated custom onConfigurationChanged()
method, or is it something simpler? Thanks.

- 199
- 2
- 12
3 Answers
They handle config changes themselves. The activity is setup as follows
<activity
android:configChanges="orientation|uiMode|screenSize|fontScale"
android:screenOrientation="user"
...
and then implements onConfigurationChanged
. In there they most likely do extensive layout animations so it looks smooth.

- 63,179
- 10
- 123
- 154
-
Does this imply that we cannot avoid it if we are using a Google Maps fragment? – ThePartyTurtle Nov 07 '18 at 21:08
-
@ThePartyTurtle depends on whether you can live with the default behavior / orientation change handling or not. That's why there is https://developer.android.com/guide/topics/resources/runtime-changes – zapl Nov 07 '18 at 21:19
-
Right, I was curious if you were implying that google has implemented "configChanges" flags in their MapFragment that would prevent a developer from keeping the MapFragment from refreshing itself. I can put that flag on my parent activity, but if Google is forcing a refresh through handling of config changes at the fragment level then I'd be out of luck I would guess. – ThePartyTurtle Nov 08 '18 at 14:58
-
`onConfigurationChanged` crashed my app. Might want to add a hint you need to call `super.onConfigurationChanged(newConfig)` on the first line – xjcl May 24 '20 at 22:54
Try using an onSaveInstanceState method in your activity and restore your values in onRestoreInstanceState. This is the example in Android developers page:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
super.onSaveInstanceState(savedInstanceState);
}
And then:
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}
Check the documentation here -> http://developer.android.com/intl/es/training/basics/activity-lifecycle/recreating.html

- 922
- 12
- 38
-
I do that, but that is still creating a new Activity from a saved instance state, so the jarring transition is still there. – newt Nov 03 '15 at 19:47
-
Did you see the documentation? Maybe you have to read about the cycle life of the activity or another solution it is to put the orientation of your activity in your manifest. For example in my cellphone when I open the google maps I cant change the orientation and my cellphone is not small. – LordCommanDev Nov 03 '15 at 19:54
-
Yes. But If you look at the Google Maps app, the map does not refresh itself even upon orientation change. You are just locking orientation change; that is not the same thing. – newt Nov 03 '15 at 19:57
Add to your AndroidManifest.xml
inside the relevant <activity>
tag:
<activity
...
android:configChanges="keyboardHidden|orientation|screenSize"
...>
Note that this means that onCreate
will no longer be called on orientation change. If you still need a callback, use onConfigurationChanged
like so:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// your code here
}
Additional hint: In onConfigurationChanged
your map will still expose its old projection and bounding box (from before the orientation change), in case you need to do something with the projection check my other answer.

- 12,848
- 6
- 67
- 89