[Fix]解决addObserver()不能运行在非主线程导致的crash问题

This commit is contained in:
chenfufeng
2022-03-24 16:32:36 +08:00
parent 40ab4b1083
commit 1c7520fb7c

View File

@@ -83,6 +83,16 @@ object CallerVisualAngleManager {
}
fun init(ctx: Context) {
if (Thread.currentThread() != Looper.getMainLooper().thread) {
scope.launch {
initListen(ctx)
}
} else {
initListen(ctx)
}
}
private fun initListen(ctx: Context) {
ctx.lifeCycleOwner.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Event) {
if (event == ON_DESTROY) {