1

TL;DR: According to the documentation there are two different ways to purchase a product, which do I use? Do I use an intent or IABHelper.launchPurchaseFlow()?

According to this documentation, use launchPurchaseFlow(); and attached listener to make a purchase:

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = 
    new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase)
        {
            //Item bought...? Why should I use this option
        }
};

mHelper.launchPurchaseFlow(this, SKU_GAS, 10001,
       mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");

However, according to this documentation, I must use intents and onActivityForResult() to purchase a product:

startIntentSenderForResult(pendingIntent.getIntentSender(),
       1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
       Integer.valueOf(0));
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1001) {      
        if (resultCode == RESULT_OK) {
            //Item bought...? Why should I use this option
        }
    }
}

Which method should I use to purchase the product? Both options seem extremely similar, but what is the difference, and is one better?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • This entirely depends on where you want the code to flow to. Both are perfectly valid. – OneCricketeer Aug 02 '16 at 01:51
  • Both are actually valid, it is mostly depends on preference.. I sometimes use listeners on `Adapter` which is not in the same class as the `Fragment` so that I don't pass the `Fragment` into the `Adapter` – Kevin Murvie Aug 02 '16 at 01:53
  • @cricket_007 Could you provide me with a scenario where I may want to use one option over the other? Thanks for the response. – Ruchir Baronia Aug 02 '16 at 02:00
  • Simply put: If you want to load some data in the current activity's context -- Use the listener. If you want to load that data in a different activity -- use the Intent. That's the main reason I can think of, at the moment. – OneCricketeer Aug 02 '16 at 02:02
  • @cricket_007 So I can do `startIntentSenderForResult()` in one activity, change activities, and load the data from another activity using `onActivityResult()`, right? – Ruchir Baronia Aug 02 '16 at 02:08
  • Essentially, that method *"Launches an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode"* -- meaning you need to first finish the current activity. The listener, on the otherhand will execute when the request is complete. – OneCricketeer Aug 02 '16 at 02:14
  • @TungD.Nguyen What do you mean by serving different phases? Don't they achieve the same thing at the end? Please let me know. – Ruchir Baronia Aug 02 '16 at 04:48
  • Sorry, I think I made a mistake. You can follow either tutorial. However, I think IabHelper is quite convenient because it is in Android sample code with lots of exception message that you can quickly replace with yours. – T D Nguyen Aug 02 '16 at 09:10

1 Answers1

0

It doesn't matter. If I do use the listener however, IabHelper's launchPurchaseFlow() uses startIntentSenderForResult anyway which calls onActivityResult() when it's finished. So even if I use the listener, I need the onActivityForResult, which they don't mention in the documentation. This link clears that up a bit:

onIabPurchaseFinished never called.

Community
  • 1
  • 1
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83