@@ -26,6 +26,7 @@ import com.mapbox.maps.mapbox_maps.pigeons._AnimationManager
26
26
import com.mapbox.maps.mapbox_maps.pigeons._CameraManager
27
27
import com.mapbox.maps.mapbox_maps.pigeons._LocationComponentSettingsInterface
28
28
import com.mapbox.maps.mapbox_maps.pigeons._MapInterface
29
+ import io.flutter.embedding.android.FlutterActivity
29
30
import io.flutter.plugin.common.BinaryMessenger
30
31
import io.flutter.plugin.common.MethodCall
31
32
import io.flutter.plugin.common.MethodChannel
@@ -71,6 +72,7 @@ class MapboxMapController(
71
72
*/
72
73
private class LifecycleHelper (
73
74
val parentLifecycle : Lifecycle ,
75
+ val shouldDestroyOnDestroy : Boolean ,
74
76
) : LifecycleOwner, DefaultLifecycleObserver {
75
77
76
78
val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry (this )
@@ -103,20 +105,22 @@ class MapboxMapController(
103
105
lifecycleRegistry.currentState = Lifecycle .State .CREATED
104
106
}
105
107
106
- override fun onDestroy (owner : LifecycleOwner ) {
107
- lifecycleRegistry.currentState = Lifecycle .State .DESTROYED
108
- }
108
+ override fun onDestroy (owner : LifecycleOwner ) = propagateDestroyEvent()
109
109
110
110
fun dispose () {
111
111
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
+ }
116
120
}
117
121
}
118
122
119
- private val lifecycleHelper: LifecycleHelper
123
+ private var lifecycleHelper: LifecycleHelper ? = null
120
124
121
125
init {
122
126
val mapView = MapView (context, mapInitOptions)
@@ -139,9 +143,6 @@ class MapboxMapController(
139
143
140
144
changeUserAgent(pluginVersion)
141
145
142
- lifecycleHelper = LifecycleHelper (lifecycleProvider.getLifecycle()!! )
143
- ViewTreeLifecycleOwner .set(mapView, lifecycleHelper)
144
-
145
146
StyleManager .setUp(proxyBinaryMessenger, styleController)
146
147
_CameraManager .setUp(proxyBinaryMessenger, cameraController)
147
148
Projection .setUp(proxyBinaryMessenger, projectionController)
@@ -163,11 +164,31 @@ class MapboxMapController(
163
164
return mapView
164
165
}
165
166
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
+
166
186
override fun dispose () {
167
187
if (mapView == null ) {
168
188
return
169
189
}
170
- lifecycleHelper.dispose()
190
+ lifecycleHelper?.dispose()
191
+ lifecycleHelper = null
171
192
mapView = null
172
193
mapboxMap = null
173
194
methodChannel.setMethodCallHandler(null )
0 commit comments