improve lifecycle management

This commit is contained in:
Fabian Freund
2026-03-17 15:24:44 +01:00
parent afe79ac7f7
commit e5b6d8ac25
6 changed files with 128 additions and 23 deletions
@@ -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)
}
}
@@ -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 {