从 Fragment 调用 startIntentSenderForResult (Android Billing v3) [英] Calling startIntentSenderForResult from Fragment (Android Billing v3)

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

问题描述

新的 Android Billing v3 文档和帮助程序代码在启动购买流程时使用 startIntentSenderForResult().我想从 Fragment 开始购买流程(并接收结果).

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.

例如,文档 建议致电>

For example the documentation suggests calling

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

帮助代码电话

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

调用 startIntentSenderForResult().

问题是,调用 startIntentSenderForResult() 会导致 onActivityResult() 在父 Activity 而不是 上被调用调用它的片段(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).

我可以在父 Activity 中接收 onActivityResult(),然后在 Fragment<上手动调用 onActivityResult()/code>,但是有没有办法从 Fragment 调用 startIntentSenderForResult() ,将结果直接返回给那个 Fragment's 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.

类似于:

要使用此解决方案,请在 Activity 中将 IabHelper 声明为 public,并使用一种方法从 Fragment 调用启动器.

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);
    } 
}

这篇关于从 Fragment 调用 startIntentSenderForResult (Android Billing v3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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