Cannot call removeOnDrawListener inside of onDraw
java.lang.IllegalStateException: Cannot call removeOnDrawListener inside of onDraw
at android.view.ViewTreeObserver.removeOnDrawListener(ViewTreeObserver.java:704)
at curtains.internal.NextDrawListener$onDraw$1.run(NextDrawListener.kt:27)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
android os :8.1.0 curtain:1.2.4
maybe this code not work?
mainHandler.post {
view.viewTreeObserver.let { viewTreeObserver ->
if (viewTreeObserver.isAlive) {
viewTreeObserver.removeOnDrawListener(this)
}
}
}
https://github.com/square/curtains/blob/main/curtains/src/main/java/curtains/internal/NextDrawListener.kt#L27
The stacktrace indicates that the code is inside a post (android.os.Handler.dispatchMessage(Handler.java:99)) and not within a draw callback.
The exception for that version of Android is created here: https://cs.android.com/android/platform/superproject/+/android-8.1.0_r81:frameworks/base/core/java/android/view/ViewTreeObserver.java;l=704;drc=f383c24749ae6bfcf8f8cee26fe7bc6897f7511c
We can also see where mInDispatchOnDraw is set to true and false, which is within dispatchOnDraw() which does not show up in the stacktrace.
https://cs.android.com/android/platform/superproject/+/android-8.1.0_r81:frameworks/base/core/java/android/view/ViewTreeObserver.java;l=1012-1018;drc=f383c24749ae6bfcf8f8cee26fe7bc6897f7511c
So based on the code & stacktrace there's no way for mInDispatchOnDraw to be true.
Can you reproduce this issue consistently? Do you have a repro scenario with an emulator? Or is it only from production crashes? If production, is that only of a specific type of device?