5

I’d like to use the Google Play Billing Library for in-app billing in my Android app. When the app launches, I’d like to initialize the BillingClient, start the connection, and retrieve some product data. The actual purchase would happen from a different activity (not the launch activity).

Should I make some kind of singleton billing manager that contains the BillingClient, so I can start it when the app launches and still use it in the other activity? Or is there a better approach?

2 Answers 2

3

There is an example provided by Google. In this example, they use additional BillingManager class which can be accessed from different places of your application but it isn't a singleton because it's not only about BillingClient you also have to have PurchasesUpdatedListener which is receiving updates from the BillingManager.

If you want to make BillingClient a singleton you can use BillingManager from the sample app and manage the list of BillingUpdatesListener by yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

I’m not very familiar with the Android system (working on my first app made from scratch), but is it self to keep my BillingClient in a singleton for the whole lifetime of the app? Won’t it give problems with the connection etc.?
@Jonathan keeping BillingClient as a singleton for the whole lifetime of the app it's not a problem but you have to manage PurchasesUpdatedListener's in few different places where you need them. In your case in two places. First is the app start to retrieve some product data and second when you are initiating payment process.
0

You can try following approach:

mBillingManager = new BillingManager(this, getUpdateListener());

mBillingManager.startServiceConnection(new Runnable() {

    @Override
    public void run() {

        // Do AnyThing Here
      }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.