Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoupling In-App-Billing from Activity

All tutorials I could find on Android In-App-Billing V3 assume you have one single activity that handles everything billing related. In my case there are multiple activities which will need access to the billing. How would I handle such a thing most elegantly?

One example problem I stumbled upon: When working with the Google billing helper classes you always hand over the current activity as a parameter. Later callbacks (e.g. onActivityResult) are called on that activity. But what if the active activity changes all the time? Do I have to shut down and re-initialize the billing all the time?

like image 251
Boris Avatar asked Aug 06 '13 20:08

Boris


1 Answers

But what if the active activity changes all the time? Do I have to shut down and re-initialize the billing all the time?

There is nothing bad in it. Connecting to a service is very fast. Most important is to be able to handle onActivityResult() callback when activity starts again.

How would I handle such a thing most elegantly?

I'm not sure which kind of application you write. If it's a game, then most likely it consists of a single activity and there is no problem anyway. If it is other kind of application with multiple activities, then in my opinion, it's a good idea to have a single activity, where user is able to see all the in-app products (bought and to buy). This is like an "internal store" activity. This activity can connect to the billing service. Other activities should forward to "internal store" where user can read more about an in-app product and decide to buy it. I find it very convenient.

Another approach would be to implement you billing logic in a Fragment which can be reused in every activity. You just need to override onActivityResult() and forward result to that Fragment. This is how I implemented it in my app.

Hope this helps.

like image 122
sergej shafarenka Avatar answered Sep 21 '22 04:09

sergej shafarenka