I have the following code which is working as expected but when I request to remove location updates using the stopLocationUpdates method shown below, my onCompleteListener attached to removeLocationUpdates doesn't get triggered on the first request despite the location updates stopped as required. However, on next request, the onCompleteListener callback is triggered.
/**
* Provides access to the fused location provider API
*/
private FusedLocationProviderClient mFusedLocationClient;
/**
* Removes location updates from the FusedLocationApi
*/
private void stopLocationUpdates() {
if (!mRequestingLocationUpdates) {
// Updates were never requested.
return;
}
// Remove location request when activity is in a paused or stopped state.
mFusedLocationClient.removeLocationUpdates(mLocationCallback)
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mRequestingLocationUpdates = false;
}
});
}