anjlab / android-inapp-billing-v3

A lightweight implementation of Android In-app Billing Version 3
Other
2.2k stars 536 forks source link

v3 onProductPurchase() called in another activity instead of the current one #503

Open goman64 opened 3 years ago

goman64 commented 3 years ago

I have updated to 2.0.0.

I have two instances of BillingProcessor in 2 activities: FirstActivity and SecondActivity. Both activities allow different in-app purchases. However, when FirstActivity calls SecondActivity and a purchase is made on the latter, the onProductPurchase() of FirstActivity is being called. I did not have this issue earlier. Kindly advise.

kcochibili commented 2 years ago

Hello, I had the same issue, to fix it , I created this singleton class that automatically holds a reference to the BillingProcessor instance. it should fix your issue, check it out.

Here is an example usage below:

Example 1: put this in the onResume method of every activity that uses billing:

    @Override
    protected void onResume() {
        super.onResume();

        BillingHolder.getInstance().getBp(this); // must be in every activity that uses billing

    }

to use this class, simply use it directly like below. BillingHolder.getInstance().getBp(this).purchase(this, "food");

Example 2: put this on the onResume method of every activity that uses billing:

        public BillingProcessor bp;

    @Override
    protected void onResume() {
        super.onResume();

        bp = BillingHolder.getInstance().getBp(this); // must be in every activity that uses billing

    }

usage: bp.purchase(this, "food");

svenoaks commented 2 years ago

@kcochibili pretty sure that singleton is leaking the activity on screen rotation and also the callbacks will stop working

kcochibili commented 2 years ago

@svenoaks can you specify where in the Singleton code you think could need improvement to prevent leaking?

svenoaks commented 2 years ago

@kcochibili I think calling killLast() in onDestroy() of every Activity would prevent it. You did make that public so it's fine, but not mentioned in the gist.

Mohamedkivin commented 2 years ago

Can you help me build in app billing 3 please؟