Skip to content

Handle device orientation change

nutiteq edited this page Aug 14, 2012 · 2 revisions

You most probably want to handle device auto-rotation (orientation change), so map view coordinates (and other settings like layers) are not reset with every onCreate. One way to do it is with NonConfigurationInstance:

1. Save state: add following method to your Activity

    @Override
    public Object onRetainNonConfigurationInstance() {
        Log.debug("onRetainNonConfigurationInstance");
        return this.mapView.getComponents();
    }

2. Restore state: load Components from the NonConfigurationInstance if available (i.e. if there was orientation flip). Otherwise, if it was first view, create new

        // following goes normally to onCreate() of your Activity with map
        Components retainObject = (Components) getLastNonConfigurationInstance();
        if (retainObject != null) {
            // just restore configuration, skip other initializations
            mapView.setComponents(retainObject);
            mapView.startMapping();
        } else {
            mapView.setComponents(new Components());
            // here will be all other MapView initial configurations
            // ...

        }
Clone this wiki locally