Skip to content

Commit 8ae0752

Browse files
feat: Lighten MainApplication.onCreate for faster startup
- Moves non-critical initializations (ANR watchdog, worker scheduling) to background threads. - Uses lazy initialization for the ANR watchdog to avoid upfront object creation. - Moves theme configuration (which involves disk I/O) off the main thread. - Adds `android.os.Trace` calls to `onCreate` for startup performance profiling. - Moves API client initialization to a background thread.
1 parent 83a17ba commit 8ae0752

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

app/src/main/java/org/ole/planet/myplanet/MainApplication.kt

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.content.Intent
77
import android.content.SharedPreferences
88
import android.content.res.Configuration
99
import android.os.Bundle
10+
import android.os.Trace
1011
import android.os.StrictMode
1112
import android.os.StrictMode.VmPolicy
1213
import android.provider.Settings
@@ -176,24 +177,37 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
176177
private var activityReferences = 0
177178
private var isActivityChangingConfigurations = false
178179
private var isFirstLaunch = true
179-
private lateinit var anrWatchdog: ANRWatchdog
180+
private var anrWatchdogStarted = false
181+
private val anrWatchdog: ANRWatchdog by lazy {
182+
ANRWatchdog(timeout = 5000L, listener = object : ANRWatchdog.ANRListener {
183+
override fun onAppNotResponding(message: String, blockedThread: Thread, duration: Long) {
184+
applicationScope.launch {
185+
createLog("anr", "ANR detected! Duration: ${duration}ms\n $message")
186+
}
187+
}
188+
})
189+
}
180190

181191
override fun onCreate() {
182192
super.onCreate()
193+
Trace.beginSection("MainApplication.onCreate")
183194
initApp()
184195
setupCriticalProperties()
185-
ensureApiClientInitialized()
186196
setupStrictMode()
187197
registerExceptionHandler()
188198
setupLifecycleCallbacks()
189199
configureTheme()
190200

191201
applicationScope.launch {
202+
ensureApiClientInitialized()
192203
initializeDatabaseConnection()
204+
observeNetworkForDownloads()
205+
}
206+
applicationScope.launch(Dispatchers.Default) {
193207
setupAnrWatchdog()
194208
scheduleWorkersOnStart()
195-
observeNetworkForDownloads()
196209
}
210+
Trace.endSection()
197211
}
198212

199213
private fun initApp() {
@@ -239,14 +253,10 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
239253

240254
private suspend fun setupAnrWatchdog() {
241255
withContext(Dispatchers.Default) {
242-
anrWatchdog = ANRWatchdog(timeout = 5000L, listener = object : ANRWatchdog.ANRListener {
243-
override fun onAppNotResponding(message: String, blockedThread: Thread, duration: Long) {
244-
applicationScope.launch {
245-
createLog("anr", "ANR detected! Duration: ${duration}ms\n $message")
246-
}
247-
}
248-
})
249-
anrWatchdog.start()
256+
if (!anrWatchdogStarted) {
257+
anrWatchdog.start()
258+
anrWatchdogStarted = true
259+
}
250260
}
251261
}
252262

@@ -276,8 +286,12 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
276286
}
277287

278288
private fun configureTheme() {
279-
val savedThemeMode = getCurrentThemeMode()
280-
applyThemeMode(savedThemeMode)
289+
applicationScope.launch(Dispatchers.IO) {
290+
val savedThemeMode = getCurrentThemeMode()
291+
withContext(Dispatchers.Main) {
292+
applyThemeMode(savedThemeMode)
293+
}
294+
}
281295
}
282296

283297
private suspend fun observeNetworkForDownloads() {
@@ -392,7 +406,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
392406
}
393407

394408
override fun onTerminate() {
395-
if (::anrWatchdog.isInitialized) {
409+
if (anrWatchdogStarted) {
396410
anrWatchdog.stop()
397411
}
398412
super.onTerminate()

0 commit comments

Comments
 (0)