views:

33

answers:

1

I have a mapView activity that has a LocationListener registered to check for a new location every two seconds. When I press the back button to go back to the main screen of the app the gps continues to check for a location every two seconds. Does the listener not get stopped when the Activity stops? If not how can I handle this? I currently have not overridden onStop().

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {
        //TODO do something to handle when the provider is disabled
    }

    public void onProviderEnabled(String provider) { }
    public void onStatusChanged(String provider, int status, Bundle extras) { }
};
+3  A: 

You need to ovverride the onPause method of your Activity because that is sure to be called. In the onPause method you should unregister the locationListener.

You do this through

locationManager.removeUpdates(yourListener);

If you have a mapView I sometimes had the problems with the locationOverlay keeping a listener registered and using the GPS even if the mapview was in the background.

Janusz
Worked like a charm. Thanks
Mike