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
This commit is contained in:
birdbird committed 2026-09-29 14:12:09 +02:00
1 parent 037787819d
commit a921ff8ea8
1 file changed
+11 -3
@@ -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
}
}