@@ -4,18 +4,30 @@ import androidx.lifecycle.DefaultLifecycleObserver
44import androidx.lifecycle.LifecycleOwner
55
66/* *
7- * Cancels in-flight location requests when the app enters background.
7+ * Manages location lifecycle tied to app foreground/ background transitions .
88 *
99 * Registered with [ProcessLifecycleOwner] during module initialization.
10- * [onStop] fires when the app transitions to background — any active
11- * GPS request is cancelled to avoid unnecessary work while backgrounded.
10+ * - [onStart]: triggers a one-shot location request on the first foreground entry
11+ * when [trackingMode] is [LocationTrackingMode.ON_APP_START].
12+ * - [onStop]: cancels any in-flight GPS request when the app enters background.
1213 *
13- * No mutable state — thread safety is inherent.
14+ * Thread safety: all lifecycle callbacks are delivered on the main thread
15+ * by [ProcessLifecycleOwner], so no synchronization is needed.
1416 */
1517internal class LocationLifecycleObserver (
16- private val locationServices : LocationServicesImpl
18+ private val locationServices : LocationServicesImpl ,
19+ private val trackingMode : LocationTrackingMode
1720) : DefaultLifecycleObserver {
1821
22+ private var hasRequestedOnStart = false
23+
24+ override fun onStart (owner : LifecycleOwner ) {
25+ if (trackingMode == LocationTrackingMode .ON_APP_START && ! hasRequestedOnStart) {
26+ hasRequestedOnStart = true
27+ locationServices.requestLocationUpdate()
28+ }
29+ }
30+
1931 override fun onStop (owner : LifecycleOwner ) {
2032 locationServices.cancelInFlightRequest()
2133 }
0 commit comments