improve lifecycle management
This commit is contained in:
+4
-2
@@ -18,13 +18,15 @@ import io.flutter.plugin.platform.PlatformView
|
||||
import io.flutter.plugin.platform.PlatformViewFactory
|
||||
|
||||
class GeckoViewFactory(
|
||||
private val activity: Activity,
|
||||
private val activityProvider: () -> Activity?,
|
||||
private val containerId: Int,
|
||||
private val flutterEvents: GeckoStateEvents
|
||||
) : PlatformViewFactory(
|
||||
StandardMessageCodec.INSTANCE) {
|
||||
override fun create(context: Context?, id: Int, args: Any?): PlatformView {
|
||||
return NativeFragmentView(this.activity, this.containerId, this.flutterEvents)
|
||||
val activity = activityProvider()
|
||||
?: throw IllegalStateException("No activity available when creating GeckoView platform view")
|
||||
return NativeFragmentView(activity, this.containerId, this.flutterEvents)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-11
@@ -134,26 +134,27 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
|
||||
_flutterPluginBinding = flutterPluginBinding
|
||||
_flutterEvents = GeckoStateEvents(_flutterPluginBinding.binaryMessenger)
|
||||
|
||||
// Register platform view factory once per engine binding.
|
||||
// The factory resolves the current activity lazily via activityProvider,
|
||||
// so it always uses the latest activity after recreation/config changes.
|
||||
_flutterPluginBinding.platformViewRegistry.registerViewFactory(
|
||||
"eu.weblibre/gecko", GeckoViewFactory(
|
||||
activityProvider = { this.activity },
|
||||
FRAGMENT_CONTAINER_ID,
|
||||
_flutterEvents
|
||||
)
|
||||
)
|
||||
isPlatformViewRegistered = true
|
||||
|
||||
isGeckoInitialized = false
|
||||
}
|
||||
|
||||
fun attachActivity(activity: Activity) {
|
||||
this.activity = activity
|
||||
|
||||
_flutterPluginBinding.platformViewRegistry.registerViewFactory(
|
||||
"eu.weblibre/gecko", GeckoViewFactory(
|
||||
activity,
|
||||
FRAGMENT_CONTAINER_ID,
|
||||
_flutterEvents
|
||||
)
|
||||
)
|
||||
|
||||
isPlatformViewRegistered = true
|
||||
}
|
||||
|
||||
fun detachActivity() {
|
||||
this.activity = null
|
||||
isPlatformViewRegistered = false
|
||||
}
|
||||
|
||||
override fun getGeckoVersion(): String {
|
||||
|
||||
Reference in New Issue
Block a user