使用应用内计费时,我应该在 activityResult 上设置内容视图吗

should I set the content view on activityResult when working with in-app-billing

我有一个关于处理用户购买的问题。就我而言,我想在用户购买时删除添加。我有两个版本的 activity_main 布局 - 一个有添加,一个没有。我想知道用户购买时在哪里以及如何设置布局。 我在想这样的事情:

if (adsDisabled == true){
setContentView(R.layout.mainNoAds)
} else{
setContentView(R.layout.main
}

我私有变量监听器的代码:

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

我在点击菜单项时调用它:

public boolean onOptionsItemSelected(MenuItem item) {
if(id == R.id.action_remove_adds){
        mHelper.launchPurchaseFlow(this,SKU_REMOVE_ADDS,1,mPurchasedFinishedListener,"");
    }

    return super.onOptionsItemSelected(item);
}

这是我的 onActivityResult 代码:

 @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.");
    }
}

这似乎是一种相当粗暴的方法,因为您要么在两个 (ad/non-ad) 定义之间复制相当多的布局内容,要么在另一个布局中包含一个布局。如果您只想 AdView 消失,为什么不简单地将其可见性设置为 View.GONE

一件事毫无意义,我不确定将其隐藏是否会导致广告请求停止并且 AdView 隐式暂停,因此这可能值得一看。