Skip to content

Commit 74e8a66

Browse files
authored
Fix lifecycle crash (#611)
* Fix crash when map view is sent on destroy event with cached flutter engine * Add changelog entry * Propagate destroy lifecycle event when platform view is disposed
1 parent 6464cc9 commit 74e8a66

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### main
22

33
* Support local assets for 3D puck and `ModelLayer`. To use a local assets, please specify it with `asset://` scheme in the uri.
4+
* Fix map view crashing upon host activity destruction when using a cached Flutter engine.
45

56
### 2.1.0
67

android/src/main/kotlin/com/mapbox/maps/mapbox_maps/MapboxMapController.kt

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.mapbox.maps.mapbox_maps.pigeons._AnimationManager
2626
import com.mapbox.maps.mapbox_maps.pigeons._CameraManager
2727
import com.mapbox.maps.mapbox_maps.pigeons._LocationComponentSettingsInterface
2828
import com.mapbox.maps.mapbox_maps.pigeons._MapInterface
29+
import io.flutter.embedding.android.FlutterActivity
2930
import io.flutter.plugin.common.BinaryMessenger
3031
import io.flutter.plugin.common.MethodCall
3132
import io.flutter.plugin.common.MethodChannel
@@ -71,6 +72,7 @@ class MapboxMapController(
7172
*/
7273
private class LifecycleHelper(
7374
val parentLifecycle: Lifecycle,
75+
val shouldDestroyOnDestroy: Boolean,
7476
) : LifecycleOwner, DefaultLifecycleObserver {
7577

7678
val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
@@ -103,20 +105,22 @@ class MapboxMapController(
103105
lifecycleRegistry.currentState = Lifecycle.State.CREATED
104106
}
105107

106-
override fun onDestroy(owner: LifecycleOwner) {
107-
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
108-
}
108+
override fun onDestroy(owner: LifecycleOwner) = propagateDestroyEvent()
109109

110110
fun dispose() {
111111
parentLifecycle.removeObserver(this)
112-
// fires MapView.onStop
113-
lifecycleRegistry.currentState = Lifecycle.State.CREATED
114-
// fires MapView.onDestroy
115-
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
112+
propagateDestroyEvent()
113+
}
114+
115+
private fun propagateDestroyEvent() {
116+
lifecycleRegistry.currentState = when (shouldDestroyOnDestroy) {
117+
true -> Lifecycle.State.DESTROYED
118+
false -> Lifecycle.State.CREATED
119+
}
116120
}
117121
}
118122

119-
private val lifecycleHelper: LifecycleHelper
123+
private var lifecycleHelper: LifecycleHelper? = null
120124

121125
init {
122126
val mapView = MapView(context, mapInitOptions)
@@ -139,9 +143,6 @@ class MapboxMapController(
139143

140144
changeUserAgent(pluginVersion)
141145

142-
lifecycleHelper = LifecycleHelper(lifecycleProvider.getLifecycle()!!)
143-
ViewTreeLifecycleOwner.set(mapView, lifecycleHelper)
144-
145146
StyleManager.setUp(proxyBinaryMessenger, styleController)
146147
_CameraManager.setUp(proxyBinaryMessenger, cameraController)
147148
Projection.setUp(proxyBinaryMessenger, projectionController)
@@ -163,11 +164,31 @@ class MapboxMapController(
163164
return mapView
164165
}
165166

167+
override fun onFlutterViewAttached(flutterView: View) {
168+
super.onFlutterViewAttached(flutterView)
169+
val context = flutterView.context
170+
val shouldDestroyOnDestroy = when (context is FlutterActivity) {
171+
true -> context.shouldDestroyEngineWithHost()
172+
false -> true
173+
}
174+
lifecycleHelper = LifecycleHelper(lifecycleProvider.getLifecycle()!!, shouldDestroyOnDestroy)
175+
176+
mapView?.let { ViewTreeLifecycleOwner.set(it, lifecycleHelper) }
177+
}
178+
179+
override fun onFlutterViewDetached() {
180+
super.onFlutterViewDetached()
181+
lifecycleHelper?.dispose()
182+
lifecycleHelper = null
183+
ViewTreeLifecycleOwner.set(mapView!!, null)
184+
}
185+
166186
override fun dispose() {
167187
if (mapView == null) {
168188
return
169189
}
170-
lifecycleHelper.dispose()
190+
lifecycleHelper?.dispose()
191+
lifecycleHelper = null
171192
mapView = null
172193
mapboxMap = null
173194
methodChannel.setMethodCallHandler(null)

0 commit comments

Comments
 (0)