问题描述
在我的应用程序中,我想使用InAppBillling购买一些功能!
我将付款方式写入fragment
但是在显示付款dialog
并由用户付款之后,我没有向onActivityResult
显示任何数据!
我将log.e
写入onActivityResult
,但再次未显示。
我认为不参加onActivityResult
!
我的片段代码:
public class ServicesFragment extends BaseFragment implements IabHelper.OnIabSetupFinishedListener,
IabHelper.QueryInventoryFinishedListener, IabHelper.OnIabPurchaseFinishedListener, IabHelper.OnConsumeFinishedListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_services, container, false);
...
}
@Override
public void onIabSetupFinished(IabResult result) {
if (iabHelper != null) {
iabHelper.flagEndAsync();
//This is call for payment
iabHelper.queryInventoryAsync(ServicesFragment.this);
}
}
@Override
public void onIabPurchaseFinished(IabResult result, final Purchase info) {
//After payment call this
if (result.isSuccess() && info != null) {
handler.post(new Runnable() {
@Override
public void run() {
getCheckBuyFromBazaar(jwtToken, packageName, productId, purchaseToken, buyID, 0, info);
}
});
}
}
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
if (result.isSuccess()) {
bazaarProgressDialog.dismiss();
//Run bazaar dialog
if (iabHelper != null) {
iabHelper.flagEndAsync();
iabHelper.launchPurchaseFlow(activity, bazaarSKU, bazaarRequestCode,
ServicesFragment.this, bazaarHashCode);
}
}
}
@Override
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess() && purchase != null) {
if (purchase.getSku().equalsIgnoreCase(bazaarSKU)) {
//When purchase consumed
finishPage();
loadingProgressDialog.dismiss();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("BazaarPaymentLog", "Result");
if (iabHelper == null) return;
if (requestCode == bazaarRequestCode) {
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
purchaseToken = jo.getString("purchaseToken");
productId = jo.getString("productId");
packageName = jo.getString("packageName");
Log.e("BazaarPaymentLog", "purchaseToken : " + purchaseToken);
if (iabHelper != null) {
iabHelper.handleActivityResult(requestCode, resultCode, data);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
显示付款对话框,然后我付款,但是不转到onActivityResult
!
我将上面的代码写到activity
就可以了,然后运行onActivityResult
但是fragment
不会进入onActivityResult
!
我该如何解决?
1楼
实际上,将结果传递给Activity的onActivtyResult以便获得Fragment的方法,您需要将reult fron Activity传递给Fragment,如下所示
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame);
fragment.onActivityResult(requestCode, resultCode, data);
}
并在Fragment中调用它与您当前正在调用的相同
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
如果您使用的是ViewPager,则可以获取如下片段
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment mFragment = myPagerAdapter.getItem(mViewPager.getCurrentItem());
if (fragment instanceof ServiccesFragment){
((ServiccesFragment)fragment).onActivityResult(requestCode, resultCode, data);
}
}