MapViewLifecycleHelper
Helper for resetting the MSDK MapView to tracking mode after guidance ends on Android Auto.
Unlike the phone app where the MapView is destroyed and recreated when switching screens, the Android Auto MapView persists throughout the session. After guidance ends, explicit cleanup is needed to:
Clear stale guidance overlays (route arrows, turn instruction overlays)
Reset the navigation session to tracking mode
Start a new session that follows the user's GPS position
Why This Is Needed
When guidance stops, GuidanceViewModel.stopGuidance() dispatches stopGuidanceUseCase() asynchronously on a background scope. The resetToTrackingMode method may be called before that async stop completes. Starting a new session while the MSDK is mid-stop causes native deadlocks and MediaPlayerNative errors. This helper safely waits for the session to reach IDLE before proceeding.
Native Overlay Cleanup
SessionState.IDLE indicates the session state machine has transitioned, but the native rendering pipeline may still be processing cleanup of guidance graphics (route arrows on the road). A configurable delay after IDLE gives the renderer time to finish before a new session starts.
Threading
All methods in this helper are suspend functions and must be called from a coroutine on Dispatchers.IO (NOT Dispatchers.Main). The MSDK's Navigation.startSession() and stopSession() are blocking native JNI calls that would cause ANR on the main thread.
See also
Functions
Resets the MSDK navigation to tracking mode after guidance ends.