Search code examples
androidkotlinin-app-billing

Android InAppBilling - How do I check if a product was purchased from another activity?


I implemented successfully In App Billing using the InAppBilling-V3 library.

I have implemented all of the in app billing within my ProActivity, once a purchase is completed successfully I set a boolean value in the shared preferences to true.

I know that shared preferences are easily modifiable by the user, so I was looking for a way to double check from my MainActivity if the product was purchased.

I would need basically to call

billingProcessor.listOwnedProducts() 

and check that the list is != 0, but I need to do it from my MainActivity which is not the one I implemented in app billing in.

Is there a way to achieve that? I couldn't find anything in the documentation. All of the docs do it from the activity where the in app billing is implemented.


Solution

  • You are right the boolean in shared preferences is unsafe and you will have to check in each activity that needs to take a decision upon billing status.

    Two potential ways I'd do this.

    • Create an Application class and add a function to return whether an item is purchased and call this function from both your ProActivity and MainActivity classes.
    • Create a base Activity class with the function and then inherit from this class for both your ProActivity and MainActivity classes.

    I prefer the second option of creating a base class but that's subjective.