Skip to content

fix(android): keep UserLocation updating after the Activity is destroyed - #4407

Merged
mfazekas merged 1 commit into
mainfrom
fix/android-location-activity-destroy
Oct 26, 2026
Merged

mfazekas merged 1 commit into
mainfrom
fix/android-location-activity-destroy

Conversation

@mfazekas

Copy link
Copy Markdown
Contributor

Description

Fixes #4406, relates to #3965

On Android, onHostDestroy fires on every Activity destroy, not just on React teardown. It called LocationManager.destroy(), which set the process-wide start counter to -1000, so once the Activity was gone no later start() could re-enable the engine. This change removes destroy() and releases the module's count in invalidate() instead. start() also counted twice while stop() released once, so stop() never disposed the engine. It now counts once.

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder
  • I have tested the new feature on /example app.
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android

Screenshot OR Video

Tested on an Android 14 emulator with "Don't keep activities" on, using the component below with adb emu geo fix locations. The app is backgrounded (Activity destroyed, same pid) and reopened, then 3 new locations are sent:

  • Before: ReactHost.onHostDestroy → the engine's GPS registration is removed and never comes back; onUpdate count 6 → 6.
  • After: the registration comes back on resume; onUpdate count 7 → 10.

Also checked with dumpsys location:

  • Before: unmounting UserLocation (after removing the follow camera) left the engine's GPS registration in place.
  • After: the registration goes away, and comes back on remount.
  • After: plain pause/resume still updates (6 → 9).
  • After: a JS reload (R, R) keeps updates flowing, and no registration is left once everything is unmounted.

Component to reproduce the issue you're fixing

Repro4406.tsx
import { useState } from 'react';
import { Button, Text, View } from 'react-native';
import Mapbox, { MapView, Camera, UserLocation } from '@rnmapbox/maps';

import config from './utils/config'; // example app token helper

Mapbox.setAccessToken(config.get('accessToken'));

let updates = 0;

const Repro4406 = () => {
  const [count, setCount] = useState(updates);
  const [last, setLast] = useState('');
  const [userLocation, setUserLocation] = useState(true);
  const [follow, setFollow] = useState(true);
  const [mapKey, setMapKey] = useState(0);

  return (
    <View style={{ flex: 1 }}>
      <MapView key={mapKey} style={{ flex: 1 }}>
        {follow && <Camera followUserLocation followZoomLevel={16} />}
        {userLocation && (
          <UserLocation
            visible={false}
            onUpdate={(loc) => {
              updates += 1;
              const s = `${loc.coords.latitude.toFixed(5)},${loc.coords.longitude.toFixed(5)}`;
              console.log(`REPRO4406 onUpdate #${updates} ${s}`);
              setCount(updates);
              setLast(s);
            }}
          />
        )}
      </MapView>
      <View style={{ padding: 12, backgroundColor: 'white' }}>
        <Text testID="count">onUpdate count: {count}</Text>
        <Text testID="last">last: {last}</Text>
        <Button
          testID="toggleUL"
          title={userLocation ? 'Unmount UserLocation' : 'Mount UserLocation'}
          onPress={() => setUserLocation((v) => !v)}
        />
        <Button
          testID="toggleFollow"
          title={follow ? 'Unmount Camera follow' : 'Mount Camera follow'}
          onPress={() => setFollow((v) => !v)}
        />
        <Button
          testID="remountMap"
          title="Remount map"
          onPress={() => setMapKey((k) => k + 1)}
        />
      </View>
    </View>
  );
};

export default Repro4406;

onHostDestroy fires on every Activity destroy, not only on React teardown,
but it called LocationManager.destroy(), which pinned the process-wide
start counter at -1000, so no later start() could re-enable the engine.
Release the module's count in invalidate() instead.

start() also counted twice while stop() released once, so the engine was
never disposed after stop().

Fixes #4406
@mfazekas
mfazekas merged commit 51dee02 into main Oct 26, 2026
10 checks passed
@mfazekas
mfazekas deleted the fix/android-location-activity-destroy branch October 26, 2026 14:47

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant