Screen Rotation issues when targeting Android API levels greater than 13

587
4
04-04-2013 05:37 AM
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
I recently set my app to target Android API level 14(ICS).  Now when I rotate the map I lose any graphics on a graphics layer and I lose my callout if visible.   I've narrowed down the problem to the onRetainNonConfigurationInstance() method.  I checked the Android API documentation and It has been depreciated at level 13 in favor of using Fragment.setRetainInstance(true);.  Well, I'm not using Fragments.

/** Called by the system, as part of destroying an activity due to a configuration change. */
    @Override
    public Object onRetainNonConfigurationInstance() {
        return mMapView.retainState();
    }


Android API Documentation:  http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance()


H
ow can I target API level 14 and retain my map state without using Fragments?
0 Kudos
4 Replies
MilesWacker
New Contributor
This has long been an issue, is there a resolution?  (that doesn't include, at the next release...)
0 Kudos
AndyGup
Esri Regular Contributor
By default, the android.view.View is destroyed whenever the phone rotates or you transition to another view. I disagree with that design, but it's fundamental to the Android OS, and it even affects web apps running in the phone's browser. Here's a few ideas that I've used before:

1) Until you can migrate to Fragments you can "temporarily" continue to use onRetainNonConfigurationInstance() with your current architecture. That's not ideal, of course, but the functionality is deprecated but not gone and removed from the native SDK...yet.

2) You can prevent rotation of the screen but not allowing it in the manifest. I've seen this most often on phones where viewing the map in portrait mode makes the most sense and best use of screen real estate. I understand that might not work with your requirements.

3) **Make sure you set the android:configChanges property in the manifest's Activity tag. For example, android:configChanges="orientation". This prevents the onCreate event from firing every time the phone rotates. Why? Because every time onCreate fires then depending on how you architected your code it could be rebuilding (or partially rebuilding) your map.

You can also do the following if #2 above doesn't resolve your issues:

1) Store any important map or graphic layer values in SQLite or in a singleton
2) Listen for rotation events
3) when phone rotates rebuild the map using the values stored in step #1.

Again, this isn't ideal but the pattern can give you a framework for attempting to maintain map state.

-Andy


-Andy
0 Kudos
JuliusBagdonas
New Contributor
android:configChanges="orientation|screenSize"


Add this bit of code to your manifest -> activity.

In android 4.x screenSize flag is a must, as it also triggers activity to be recreated.

Edit: Also, it would be great if someone updated SDK samples to include this for android 4.x ...
0 Kudos
DanO_Neill
Occasional Contributor III
android:configChanges="orientation|screenSize"


Add this bit of code to your manifest -> activity.

In android 4.x screenSize flag is a must, as it also triggers activity to be recreated.

Edit: Also, it would be great if someone updated SDK samples to include this for android 4.x ...


If your application targets API level 13 or higher you should declare the "screenSize" configuration, becuase it changes when a device switches between portrait and landscape.  We have added it to SDK samples targeting API level 13 and higher.  Here is the android guide topic.
0 Kudos