hotchemi / Android-Rate

Android-Rate is a library to help you promote your android app by prompting users to rate the app after using it for a few days.
http://hotchemi.github.io/Android-Rate
MIT License
724 stars 216 forks source link

onClickButton() callback may not happen always #115

Open rpattabi opened 7 years ago

rpattabi commented 7 years ago

I found that my callback for onClickButton() did not happen. I just followed what is recommended in README.

            AppRate.with(this)
                    .setDebug(BuildConfig.DEBUG)
                    .setInstallDays(5)
                    .setLaunchTimes(4)
                    .setRemindInterval(3)
                    .setShowLaterButton(true)
                    .setOnClickButtonListener(new OnClickButtonListener() {
                        @Override
                        public void onClickButton(int which) {
==>                         Log.d("Rate", "onClickButton: " + which);
                        }
                    })
                    .monitor();
rpattabi commented 7 years ago

From the code, I see that the listener is weak referenced. Since we set the listener at onCreate() and the dialog only pops up when we close the app, it is likely that OnClickButtonListener object is garbage collected since there is no strong reference to this object. If the object is gone, the callback won't happen.

I think the weak reference is used mainly with the assumption that the activity itself is the listener. So the work around is to make activity implement OnClickButtonListener interface and set listener like this: .setOnClickButtonListener(this)

I confirm if Activity itself implements OnClickButton interface, the callback works correctly. But this shall be fixed if my above understanding is correct. It is very common to create listeners inline in java.

AlexanderLS commented 6 years ago

Hi @rpattabi ,

Fixed in https://github.com/Vorlonsoft/AndroidRate since Release 1.2.1