Question

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?

Était-ce utile?

La solution

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.

Autres conseils

Think a bit outside of the example box. It is not related to your problem only, but is general.

I would use a notification system, because you have 1 publisher and many listeners needed(your case 2). One, ugliest method can be ( but fastest to write) :

  1. make a fake activity (even invisible, whatever)
  2. copy-paste the examples code which works there
  3. hack a bit that working code and add listeners, which takes parameters as your real activity
  4. notify when needed your instances, when needed

5th up-vote if helped or liked :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top