Vorlonsoft / AndroidRate

AndroidRate is a library to help you promote your Android app by prompting users to rate the app after using it for a few days.
https://vorlonsoft.github.io/AndroidRate/
MIT License
136 stars 34 forks source link

Singleton DialogManager break the library #6

Closed devnied closed 6 years ago

devnied commented 6 years ago

When we want to show the rate dialog in multiple Activity or in the same activity, if the first activity that call AppRate.showRateDialogIfMeetsConditions(this); is destroyed. The next call will fail with:

java.lang.RuntimeException: Unable to resume activity {xxx/xxx.HomeActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b857bc1 is not valid; is your activity running?
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3454)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3494)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6186)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b857bc1 is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
        at android.app.Dialog.show(Dialog.java:329)
        at com.vorlonsoft.android.rate.AppRate.showRateDialog(AppRate.java:413)
        at com.vorlonsoft.android.rate.AppRate.showRateDialogIfMeetsConditions(AppRate.java:114)
        at xxx.activity.HomeActivity.onResume(HomeActivity.java:411)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1270)
        at android.app.Activity.performResume(Activity.java:6788)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3431)

The DialogManager Singleton hold the first activity as context and reuse it even it was destroyed.
If you want to keep the singleton, I think the createDialogManager function must check if the singleton context is not destroyed before reuse it.

This issue is linked to #5. ps: I have tested with the version 1.1.9 and with the latest commit into the master branch. Thanks,

AlexanderLS commented 6 years ago

Hi @devnied,

Thank you!

It's my mistake. I was so sure that it was Application Context that I didn't even look at the code. Key moment - it's Activity Context, not Application Context.

Fixed in LIBbb20831