diff --git a/app/src/main/java/io/legado/app/utils/HandlerUtils.kt b/app/src/main/java/io/legado/app/utils/HandlerUtils.kt index 0eacea43b..3ce76e5ca 100644 --- a/app/src/main/java/io/legado/app/utils/HandlerUtils.kt +++ b/app/src/main/java/io/legado/app/utils/HandlerUtils.kt @@ -11,7 +11,7 @@ import kotlinx.coroutines.launch /** This main looper cache avoids synchronization overhead when accessed repeatedly. */ @JvmField -val mainLooper: Looper = Looper.getMainLooper()!! +val mainLooper: Looper = Looper.getMainLooper() @JvmField val mainThread: Thread = mainLooper.thread @@ -22,15 +22,17 @@ val isMainThread: Boolean inline get() = mainThread === Thread.currentThread() internal val currentThread: Any? inline get() = Thread.currentThread() -@JvmField -val mainHandler: Handler = if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try { - Handler::class.java.getDeclaredConstructor( - Looper::class.java, - Handler.Callback::class.java, - Boolean::class.javaPrimitiveType // async - ).newInstance(mainLooper, null, true) -} catch (ignored: NoSuchMethodException) { - Handler(mainLooper) // Hidden constructor absent. Fall back to non-async constructor. +val mainHandler: Handler by lazy { + if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try { + Handler::class.java.getDeclaredConstructor( + Looper::class.java, + Handler.Callback::class.java, + Boolean::class.javaPrimitiveType // async + ).newInstance(mainLooper, null, true) + } catch (ignored: NoSuchMethodException) { + // Hidden constructor absent. Fall back to non-async constructor. + Handler(mainLooper) + } } fun runOnUI(function: () -> Unit) {