[Bug]: UserLocation stops updating after restarting app with background foregroundService on Android
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 2.9k
- Forks
- 947
- Avg merge
- 6d 37m
- Merged PRs (30d)
- 1
Description
Mapbox Implementation
Mapbox GL
Mapbox Version
default
React Native Version
0.78.2
Platform
Android
@rnmapbox/maps version
10.1.42
Standalone component to reproduce
import React, {useState} from "react";
import MapboxGL, {Camera, MapView, UserLocation} from "@rnmapbox/maps";
import {ActivityIndicator, Platform, View} from "react-native";
Platform.OS === "android" && MapboxGL.requestAndroidLocationPermissions();
export const START_ZOOM_LEVEL = 15;
export const MAX_ZOOM_LEVEL = 18;
export const MIN_ZOOM_LEVEL = 1;
const DEFAULT_LOCATION = {
latitude: 52.52,
longitude: 13.405,
};
export default function AppMap() {
const [mapLoaded, setMapLoaded] = useState(false);
return (
<View style={{flex: 1}}>
{!mapLoaded && (
<View
style={{
position: "absolute",
zIndex: 200,
bottom: "50%",
alignSelf: "center",
}}>
<View
style={{
backgroundColor: "white",
padding: 8,
borderRadius: 9999,
}}>
<ActivityIndicator size={48} />
</View>
</View>
)}
<MapView
logoEnabled={true}
attributionEnabled={true}
rotateEnabled={false}
zoomEnabled
scrollEnabled
pitchEnabled={false}
scaleBarEnabled={false}
onDidFinishLoadingMap={() => {
setMapLoaded(true);
}}
style={{flex: 1}}>
<>
<Camera
minZoomLevel={MIN_ZOOM_LEVEL}
maxZoomLevel={MAX_ZOOM_LEVEL}
allowUpdates={true}
animationMode="none"
defaultSettings={{
centerCoordinate: [
DEFAULT_LOCATION.longitude,
DEFAULT_LOCATION.latitude,
],
zoomLevel: START_ZOOM_LEVEL,
}}
/>
<UserLocation
androidRenderMode="normal"
requestsAlwaysUse
minDisplacement={10}
visible={true}
onUpdate={res => {
console.log("User location updated:", res);
}}
/>
</>
</MapView>
</View>
);
}
Observed behavior and steps to reproduce
Steps to reproduce
- Open the app and display the map with UserLocation.
- Start some kind of a android foregroundService that works even when the app is closed
- Restart the app.
- Observe that the user location updates only a couple of times and then stops.
- Switch to the deprecated renderMode={UserLocationRenderMode.Native} and notice that the location updates correctly, but onUpdate remains non-functional.
Expected behavior
The map should continuously update the user’s location while the foreground service is running, even after the app is restarted.
Notes / preliminary analysis
When I start a foregroundService with android:foregroundServiceType="location", my location on the map stops updating after restarting the app. The position is retrieved at most twice and then stops completely.
If I use the deprecated renderMode={UserLocationRenderMode.Native} in UserLocation, the location on the map updates correctly, but the onUpdate callback still doesn’t return anything.
It looks like the issue is related to the interaction between Mapbox and Android foreground services. From what I’ve tested, the specific type of foregroundService doesn’t seem to make a difference.
Additional links and references
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the UserLocation component, focusing on the Android normal renderMode, onUpdate callback, and foreground-service interaction described in the reproduction. Compare this with the deprecated Native renderMode while reproducing an app restart. Done means location updates continue after restart and onUpdate remains functional while the foreground service runs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, react-native, typescript
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100