From 248c9b6fcd8fdba27cac82da9163010436668679 Mon Sep 17 00:00:00 2001 From: Miles Egan Date: Thu, 8 Aug 2024 15:33:20 +0700 Subject: [PATCH] Fix build errors in AndroidContentProvider (#19) * fix build errors in AndroidContentProvider * handle lifecycle events properly * Update pubspec and CHANGELOG. * Make lifecycleRegistry private. --- CHANGELOG.md | 4 ++++ .../AndroidContentProvider.kt | 19 +++++-------------- pubspec.yaml | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6d92a5..ce68fe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 0.4.2 + +* Fix build issue with Flutter 3.24.0. + ### 0.4.1 * Support uuid 4.0.0 diff --git a/android/src/main/kotlin/com/nt4f04und/android_content_provider/AndroidContentProvider.kt b/android/src/main/kotlin/com/nt4f04und/android_content_provider/AndroidContentProvider.kt index 3bad82c..6b535e7 100644 --- a/android/src/main/kotlin/com/nt4f04und/android_content_provider/AndroidContentProvider.kt +++ b/android/src/main/kotlin/com/nt4f04und/android_content_provider/AndroidContentProvider.kt @@ -37,7 +37,10 @@ import java.lang.Exception * Once the content provider is created, it is not destroyed, until the app process is. */ abstract class AndroidContentProvider : ContentProvider(), LifecycleOwner, Utils { - private var lifecycle: LifecycleRegistry? = null + override val lifecycle get() = lifecycleRegistry + private val lifecycleRegistry: LifecycleRegistry by lazy { + LifecycleRegistry(this) + } private lateinit var engine: FlutterEngine private lateinit var methodChannel: SynchronousMethodChannel private lateinit var trackingMapFactory: TrackingMapFactory @@ -90,26 +93,14 @@ abstract class AndroidContentProvider : ContentProvider(), LifecycleOwner, Utils */ abstract val entrypointName: String // TODO: eventually replace it with initialArguments - override fun getLifecycle(): Lifecycle { - ensureLifecycleInitialized() - return lifecycle!! - } - - private fun ensureLifecycleInitialized() { - if (lifecycle == null) { - lifecycle = LifecycleRegistry(this) - } - } - @CallSuper override fun onCreate(): Boolean { val flutterLoader = FlutterInjector.instance().flutterLoader() flutterLoader.startInitialization(context!!.applicationContext) val entrypoint = DartExecutor.DartEntrypoint(flutterLoader.findAppBundlePath(), entrypointName) val engineGroup = getFlutterEngineGroup(context!!) + lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) engine = engineGroup.createAndRunEngine(context!!, entrypoint) - ensureLifecycleInitialized() - lifecycle!!.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) engine.contentProviderControlSurface.attachToContentProvider(this, lifecycle!!) trackingMapFactory = TrackingMapFactory(engine.dartExecutor.binaryMessenger) methodChannel = SynchronousMethodChannel(MethodChannel( diff --git a/pubspec.yaml b/pubspec.yaml index c79a2b7..652327c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: android_content_provider description: Flutter plugin to use ContentProvider/ContentResolver APIs on Android -version: 0.4.1 +version: 0.4.2 homepage: https://github.com/nt4f04uNd/android_content_provider/ environment: