[英]How to use onActivityResult() in fragment?
In my application i want use InAppBillling for buy some features ! 在我的应用程序中,我想使用InAppBillling购买一些功能!
I write payment methods into fragment
but after show payment dialog
and pay by user not show me any data into onActivityResult
! 我将付款方式写入
fragment
但是在显示付款dialog
并由用户付款之后,我没有向onActivityResult
显示任何数据!
I write log.e
into onActivityResult
, but again not show. 我将
log.e
写入onActivityResult
,但再次未显示。
I think not going into onActivityResult
! 我认为不参加
onActivityResult
!
My fragment codes : 我的片段代码:
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);
}
}
}
Show me payment dialog and i pay it, but after not going to onActivityResult
! 显示付款对话框,然后我付款,但是不转到
onActivityResult
!
I write above code into activity
it's ok and run onActivityResult
but into fragment
not going into onActivityResult
! 我将上面的代码写到
activity
就可以了,然后运行onActivityResult
但是fragment
不会进入onActivityResult
!
How can i fix it? 我该如何解决?
Actually the result is passed to Activity's onActivtyResult in order to get in Fragment's method you need to pass reult fron Activity to Fragment like below 实际上,将结果传递给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);
}
and in Fragment call it same as you are calling currently 并在Fragment中调用它与您当前正在调用的相同
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
If you are using ViewPager you can get Fragment like below 如果您使用的是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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.