调用startIntentSenderForResult从片段(安卓结算V3) [英] Calling startIntentSenderForResult from Fragment (Android Billing v3)

查看:2574
本文介绍了调用startIntentSenderForResult从片段(安卓结算V3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的Andr​​oid结算v3文档资料和帮助code使用 startIntentSenderForResult()推出了购买流程时。我想开始购买流程(和接收结果)从片段

The new Android Billing v3 documentation and helper code uses startIntentSenderForResult() when launching a purchase flow. I want to start a purchase flow (and receive the result) from a Fragment.

例如在<一个href="http://developer.android.com/google/play/billing/billing_integrate.html#billing-requests">documentation建议调用

startIntentSenderForResult(pendingIntent.getIntentSender(),
    1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
    Integer.valueOf(0));

和<一href="http://developer.android.com/training/in-app-billing/purchase-iab-products.html#Purchase">helper code 电话

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

这就要求 startIntentSenderForResult()

现在的问题是,调用 startIntentSenderForResult()引起 onActivityResult()来调用父活动,而不是在片段,它是从所谓的(其中 IabHelper 所在)。

The problem is, calling startIntentSenderForResult() causes onActivityResult() to be called on the parent Activity rather than on the Fragment that it was called from (where the IabHelper resides).

我会收到 onActivityResult()活动,然后手动调用 onActivityResult()片段,但有一种方法,使呼叫 startIntentSenderForResult()片段直接将结果返回到片段 onActivityResult ()

I could receive the onActivityResult() in the parent Activity and then manually call the onActivityResult() on the Fragment, but is there a way to make a call to startIntentSenderForResult() from a Fragment that returns the result directly to that Fragment's onActivityResult()?

推荐答案

我建议两种解决方案:

1。)将IabHelper mHelper上的活动,并从片段调用IabHelper

1.) Put the IabHelper mHelper on the activity and call the IabHelper from the fragment.

是这样的:

要使用此解决方案,声明IabHelper公众的活动,并使用一个方法来调用从片段中启动。

To use this solution, Declare IabHelper as public in the activity and use a method to call the launcher from the Fragment.

public class MyActivity extends Activity{

    public IabHelper mHelper

    public purchaseLauncher(){

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

   }

    /*The finished, query and consume listeners should also be implemented in here*/
}

public class FragmentActivity extends Fragment{

      MyActivity myAct = (MyActivity) getActivity();

      myAct.purchaseLauncher();

}

2)。在onActivityResult,调用包含IabHelper对象的相应片段。合适的片段可以有一个访问方法来辅助对象。

2.) In onActivityResult, call the appropriate fragment that contains the IabHelper object. Appropriate fragment can have an access method to the helper object.

protected void onActivityResult(int requestCode, int resultCode,Intent data)    
{
    super.onActivityResult(requestCode, resultCode, data);

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag("YourTag");       
    if (fragment != null)
    {
        ((MyFragmentWithIabHelper)fragment).onActivityResult(requestCode, resultCode,data);
    } 
}

这篇关于调用startIntentSenderForResult从片段(安卓结算V3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆