From a921ff8ea80a100a1d8fa5358fdba0a8dc105f3b Mon Sep 17 00:00:00 2001 From: birdbird <6892457-tzugen@users.noreply.gitlab.com> Date: Tue, 29 Sep 2026 11:06:03 +0000 Subject: [PATCH] UApp: fix Koin readiness flag, first-run race and context accessor - initiated is now owned by startKoin() alone; setting it before super.onCreate() made the NavigationActivity Koin-restart fallback unreachable - isFirstRun is computed on the main thread: NavigationActivity.onCreate can run before the background initialization completed and read a stale 'false', silently skipping the welcome dialog on first install - applicationContext() fails with a descriptive error instead of a bare NPE when accessed before Application.onCreate --- .../main/kotlin/org/moire/ultrasonic/app/UApp.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/app/UApp.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/app/UApp.kt index bb452f37..ccf02785 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/app/UApp.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/app/UApp.kt @@ -61,7 +61,6 @@ class UApp : var setupDialogDisplayed = false override fun onCreate() { - initiated = true super.onCreate() if (BuildConfig.DEBUG) { @@ -70,6 +69,11 @@ class UApp : Timber.d("onCreate called") + // This flag flips once per install and NavigationActivity reads it while creating + // its UI, which can happen before the background initialization below completes. + // Reading it only on the main thread avoids serving a stale "false" on first run. + isFirstRun = Util.isFirstRun() + // In general we should not access the settings from the main thread to avoid blocking... ioScope.launch { if (Settings.debugLogToFile) { @@ -79,7 +83,6 @@ class UApp : // Populate externalFilesDir early FileUtil.cachedUltrasonicDirectory = FileUtil.ultrasonicDirectory Storage.mediaRoot.value - isFirstRun = Util.isFirstRun() } startKoin() @@ -113,6 +116,8 @@ class UApp : } internal fun startKoin() { + // "Koin is ready" is owned by startKoin alone; NavigationActivity relies on this + // flag to re-start Koin if the Application was recreated without a completed onCreate. initiated = true startKoin { // Sometimes Koin breaks when Kotlin version is upgraded, @@ -142,7 +147,10 @@ class UApp : companion object { var instance: UApp? = null - fun applicationContext(): Context = instance!!.applicationContext + fun applicationContext(): Context = + requireNotNull(instance) { + "UApp.instance accessed before Application.onCreate" + }.applicationContext } }