新的 Android Billing v3 文档和帮助代码使用 startIntentSenderForResult()
启动购买流程时。我想从 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()
被调用父级 Activity
而不是 Fragment
它是从(IabHelper
所在的位置)调用的。
我可以收到 onActivityResult()
在父 Activity
然后手动调用onActivityResult()
在 Fragment
,但是有没有办法调用startIntentSenderForResult()
来自 Fragment
将结果直接返回到 Fragment
的onActivityResult()
?
最佳答案
我建议两种解决方案:
1.) 将 IabHelper mHelper 放在 Activity 上,并从 Fragment 中调用 IabHelper。
类似:
要使用此解决方案,请在 Activity 中将 IabHelper 声明为公共(public),并使用方法从 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 对象的相应 fragment 。适当的 fragment 可以具有对辅助对象的访问方法。
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);
}
}
关于android - 从 Fragment 调用 startIntentSenderForResult (Android Billing v3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131171/