I have a question about handling a purchase that the user makes. In my case, I want to remove the adds when user makes a purchase. I have two versions of my activity_main layout - one with adds and one without. I am wondering where and how to set the layout when the user makes a purchase. I am thinking something like this:
if (adsDisabled == true){
setContentView(R.layout.mainNoAds)
} else{
setContentView(R.layout.main
}
the code I private variable listener:
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
@Override
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
return;
}
else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) {
// consume the purchase and update the UI
}
}
};
and I am calling it when my menu item is tapped:
public boolean onOptionsItemSelected(MenuItem item) {
if(id == R.id.action_remove_adds){
mHelper.launchPurchaseFlow(this,SKU_REMOVE_ADDS,1,mPurchasedFinishedListener,"");
}
return super.onOptionsItemSelected(item);
}
and this is my onActicityResult code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
This seems like a rather heavy-handed approach, as you're either duplicating quite a lot of your layout content between the two (ad/non-ad) definitions or include one layout in the other. If you're interested in just the AdView
going away, why not simply set its visibility to View.GONE
?
One thing worth nothing is that I'm not sure if hiding it away makes the ad requests stop and the AdView
pause implicitly, so that might be worth taking a look at.