Thursday 11 August 2011

ArcGIS Android - Persisting map state

ESRI documentation describes how to use onRetainNonConfigurationInstance for handling map rotation so that the scale, centre and layer state of the map is saved when onCreate is called (e.g http://help.arcgis.com/en/arcgismobile/10.0/apis/android/help/#/My_first_application/011900000005000000/) While this works fine I would like my application to remember map setting not only when the phone is rotated, but also when the user closes and re-enters the application. For this to work I save the map state values to the PreferenceManager like this when app is paused:
@Override
protected void onPause() {
        super.onPause();
        SharedPreferences settings = getSharedPreferences("mapPreference", 0);
        SharedPreferences.Editor ed = settings.edit();
        ed.putString("mapstate", map.retainState());
        ed.commit();
    }
 
And then retrieve the map settings in the onCreate method:
@Override
public void onCreate(Bundle savedInstanceState) {    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

// get the map settings
SharedPreferences settings = getSharedPreferences("mapPreference", 0);
         String mapState = settings.getString("mapstate", null);
         if (mapState != null){
        	map.restoreState(mapState);
         } 

No comments:

Post a Comment