skydoves / Balloon

:balloon: Modernized and sophisticated tooltips, fully customizable with an arrow and animations for Android.
https://skydoves.github.io/libraries/balloon/html/balloon/com.skydoves.balloon/index.html
Apache License 2.0
3.75k stars 291 forks source link

App Crash : android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; #39

Closed skauss closed 4 years ago

skauss commented 4 years ago

Hi, we use the Balloon library to show tooltips. The balloon is shown with a little delay of 500ms. In Firebase Crashlytics I found some crashes while showing the balloon.

It look's like the user press the back button instantly while the balloon is showing.

Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
       at android.view.ViewRootImpl.setView(ViewRootImpl.java:1056)
       at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:381)
       at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
       at android.widget.PopupWindow.invokePopup(PopupWindow.java:1478)
       at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1316)
       at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1272)
       at com.skydoves.balloon.Balloon$showAlignRight$$inlined$show$1.run(Balloon.kt:853)
       at android.os.Handler.handleCallback(Handler.java:873) 

This is happens also for

It would be great if you catch this exception

Regards Stephan

skydoves commented 4 years ago

Hi, @skauss thank you for your issue :) What attribute did you use for giving a delay to the balloon? It seems you used Handler with postDelayed.

skauss commented 4 years ago

Hi skydoves, yes I do final Handler handler = new Handler(); handler.postDelayed(() -> balloon.showAlignTop(anchor), 500);

skydoves commented 4 years ago

@skauss Ah, then you should cancel the runnable of the handler when onPause is called.

https://stackoverflow.com/questions/4378533/cancelling-a-handler-postdelayed-process