I am writing an Android application that requires the user's current location. I register for location updates both from the network, and from GPS using the following code (locationManager is already defined):
// Register the listener with the Location Manager to receive location updates.
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
getResources().getInteger(R.integer.gps_min_time),
getResources().getInteger(R.integer.gps_min_dist), this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
getResources().getInteger(R.integer.gps_min_time),
getResources().getInteger(R.integer.gps_min_dist), this);
I currently have this code in onCreate
, but in order to save power, I remove both listeners in onPause
and add them both again in onResume
.
When the application starts, it adds both listeners twice, once in onCreate
, and once in onResume
. I have two question about this:
requestLocationUpdate
s from onCreate
and just have them in onResume
or should I remove all listeners first in onResume
before adding them again?your Listener will be added as a key to a Hashmap. So If you insert this
(Your Activity) and don't override equals
it should be overridden, and has no effect, because it is the SAME Listener.
Anyway, I would remove the register inside onCreate and just leave it inside onResume, as you will save at least the time to Hash your listener and the Method calls.