Rotation changes map extent

2013
5
Jump to solution
03-13-2012 12:56 AM
BojanBukovic
New Contributor
Hello,

Here is my problem: I have a mapView and screen rotation changes the map extent. mapView is defined in main.xml layout file without initial extent. After zooming and panning and when screen rotation occurs, the map is displayed in its full extent. I do not know what did I miss. I also tried with initial extent defined in the layout file and then the map is always displayed in its initial extent after screen rotation. Can someone help me how to preserve map's extent on screen rotation?
I' ve already checked ESRI android examples which behave as expected (map extent is preserved on rotation) and I did not find the key difference in the configuration, code or map reference.

Thank you very much for any help!

Kind regards
0 Kudos
1 Solution

Accepted Solutions
SimonKlein
Occasional Contributor
Is the map just shown in full extent or totally reloaded?
Did you try my "bad" solution?

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

in the hello world sample here: http://help.arcgis.com/en/arcgismobile/10.0/apis/android/help/#/Hello_world/011900000003000000/
so does indeed recreate the activity (mapview) but zooms back to the resolution and extent you had before.

View solution in original post

0 Kudos
5 Replies
SimonKlein
Occasional Contributor
Hi,

the thing is you are missing is, that android destroys and recreates activitys on orientation change per default.
In the HelloWorld example you can see these lines in the onCreate method:
//Retrieve the non-configuration instance data that was previously returned. 
  Object init = getLastNonConfigurationInstance();
  if (init != null) {
   map.restoreState((String) init);
  }

This takes care of returning to the previous configuration of the map.

There is also the NOT recommended way of your activity just not acting on the rotation change.
Would look something  like this.
<activity android:name="YourMapActivity" android:configChanges="keyboardHidden|orientation">
0 Kudos
BojanBukovic
New Contributor
Hi,

thank you for your quick reply, but unfortunately I have to say that this is obviously not the case here.
I have this part of code in the onCreate method you mentioned.

Actually, this is my onCreate method:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.main);
        mMapView = (MapView) findViewById(R.id.map);
     mMapView.addLayer(new ArcGISDynamicMapServiceLayer("" +"http://myserver/ArcGIS/rest/services/OK_GDKarta/MapServer"));
     mMapView.addLayer(new ArcGISDynamicMapServiceLayer("" +"http://myserver/ArcGIS/rest/services/GDDynamic_EDIT/MapServer"));
               
     graphicsLayer = new GraphicsLayer();
     mMapView.addLayer(graphicsLayer);
     
    
     Object init = getLastNonConfigurationInstance();
      if (init != null) {
       mMapView.restoreState((String) init);
      }
    }



The map is still displayed in full extent on screen rotate.

Any other suggestion?

Regards
0 Kudos
SimonKlein
Occasional Contributor
Is the map just shown in full extent or totally reloaded?
Did you try my "bad" solution?

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

in the hello world sample here: http://help.arcgis.com/en/arcgismobile/10.0/apis/android/help/#/Hello_world/011900000003000000/
so does indeed recreate the activity (mapview) but zooms back to the resolution and extent you had before.

View solution in original post

0 Kudos
BojanBukovic
New Contributor
Hi,

Great, that fixes the issue. Thank you for your answer.
I should look at the samples on the website, because in the HelloWorld sample that comes with ESRI Android SDK the "issue-fix" part is missing.

Thank you once again for your help.

Regards
0 Kudos
DeniseKing
Esri Regular Contributor
If a post provides the answer to your question, then we encourage you to mark the post as "the answer" by activating the green check mark  right of the post. You can also "vote" on posts that you find helpful by clicking the blue caret mark. More information about the ArcGIS Discussion Forums MVP Program found here.

Thank you,
Esri Support
0 Kudos