From 7d8339ba3ffee0299226f0d805cd72f318d47e23 Mon Sep 17 00:00:00 2001 From: Kyle Madsen Date: Tue, 30 Aug 2022 15:30:57 -0700 Subject: [PATCH] Fix MapboxNavigation detach issue --- CHANGELOG.md | 1 + .../core/lifecycle/MapboxNavigationOwner.kt | 2 ++ .../MapboxNavigationAppDelegateTest.kt | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 050f39e91c7..c711f705ce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Mapbox welcomes participation and contributions from everyone. - Fixed incorrect values in `AlternativeRouteInfo#duration` and `RouteProgress#durationRemaining`: now they rely on `route.leg.annotations.duration` if available, otherwise, `LegStep#duration` is used for calculations. [#6237](https://github.com/mapbox/mapbox-navigation-android/pull/6237) - Added `guideMapUri` to the `RestStop`. [#6237](https://github.com/mapbox/mapbox-navigation-android/pull/6237) - [TileStore Android Service] Fixed a crash when the service process is killed by the Android system. [#6237](https://github.com/mapbox/mapbox-navigation-android/pull/6237) +- Fixed `MapboxNavigationApp#detach` will not fully detach. This causes `MapboxNavigation` to continue to be accessible, and causes `MapboxNavigationObserver.onDetached` to be called multiple times. ## Mapbox Navigation SDK 2.8.0-beta.1 - 25 August, 2022 ### Changelog diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationOwner.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationOwner.kt index c72a864fbfc..9345a1795b2 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationOwner.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationOwner.kt @@ -60,6 +60,8 @@ internal class MapboxNavigationOwner { attached = false services.forEach { it.onDetached(mapboxNavigation!!) } MapboxNavigationProvider.destroy() + mapboxNavigation = null + logI("disabled ${services.size} observers", LOG_CATEGORY) } } diff --git a/libnavigation-core/src/test/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationAppDelegateTest.kt b/libnavigation-core/src/test/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationAppDelegateTest.kt index 0a14abb66a9..aa41bb71803 100644 --- a/libnavigation-core/src/test/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationAppDelegateTest.kt +++ b/libnavigation-core/src/test/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationAppDelegateTest.kt @@ -274,6 +274,23 @@ class MapboxNavigationAppDelegateTest { verify(exactly = 1) { observer.onAttached(any()) } } + @Test + fun `verify disable will detach and current becomes null`() { + mapboxNavigationApp.setup { navigationOptions } + + val testLifecycleOwner = CarAppLifecycleOwnerTest.TestLifecycleOwner() + mapboxNavigationApp.attach(testLifecycleOwner) + testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle.State.RESUMED + + val observer = mockk(relaxUnitFun = true) + mapboxNavigationApp.registerObserver(observer) + mapboxNavigationApp.disable() + mapboxNavigationApp.unregisterObserver(observer) + + assertNull(MapboxNavigationApp.current()) + verify(exactly = 1) { observer.onDetached(any()) } + } + @Test fun `verify current is null when all lifecycle owners are destroyed`() { mapboxNavigationApp.setup { navigationOptions }