Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call removeOnDrawListener inside of onDraw #38

Open
linchangjian opened this issue Sep 8, 2022 · 3 comments
Open

Cannot call removeOnDrawListener inside of onDraw #38

linchangjian opened this issue Sep 8, 2022 · 3 comments

Comments

@linchangjian
Copy link

linchangjian commented Sep 8, 2022

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)
@linchangjian
Copy link
Author

linchangjian commented Sep 8, 2022

android os :8.1.0
curtain:1.2.4

@linchangjian
Copy link
Author

maybe this code not work?

mainHandler.post {
  view.viewTreeObserver.let { viewTreeObserver ->
    if (viewTreeObserver.isAlive) {
      viewTreeObserver.removeOnDrawListener(this)
    }
  }
}

@pyricau
Copy link
Member

pyricau commented Sep 13, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants