调用startIntentSenderForResult从片段(安卓结算V3)片段、startIntentSenderForResult

由网友(半盏流年)分享简介:新的Andr​​oid结算v3文档资料和帮助code使用 startIntentSenderForResult()推出了购买流程时。我想开始购买流程(和接收结果)从片段。The new Android Billing v3 documentation and helper code uses startIntentS...

新的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.

例如在documentation建议调用

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

和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);
    } 
}
阅读全文

相关推荐

最新文章