15

Giving another chance to this comunity, my latest questions weren't ever answered

Well I have a game, the game have a pause button that hide most of the game interface to just show a pause text in the middle. There are so much free space, so I thought to put a banner at the bottom until the pause button is pressed again and resumes the game.

I know how to make banners work:

//When pause button is pressed
AdView adView = (AdView) this.findViewById(R.id.adView);
adView.setVisibility (View.VISIBLE);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

But I don't know how to stop them when pause button is pressed again, just this:

adView.setVisibility (View.GONE);

I am pretty sure adView wont stop making requests with this line only.

I see some questions about this here but looks like they were using older admob SDK versions.

can somebody please, PLEASE help me?

Thanks.

  • looks like calling AdView::destroy() should do the trick. The destroy method is defined in android.webkit.WebView here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/webkit/WebView.java which seems to be removing all network callbacks etc. – CCJ May 08 '14 at 23:21

2 Answers2

25

You can try to Destroy and hide the AdView when button click and load the ad back when required.

    final AdView adView = (AdView) this.findViewById(R.id.adView);
    final AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    button.setText("ClickMe");
    button.setOnClickListener(new View.OnClickListener() {
         boolean isPause = false;
        @Override
        public void onClick(View v) {
            if(isPause){
                adView.loadAd(adRequest);
                adView.setVisibility(View.VISIBLE);
                isPause = false;
            }else {
                adView.destroy();
                adView.setVisibility(View.GONE);
                isPause = true;
            }
        }
    });
Libin
  • 16,967
  • 7
  • 61
  • 83
  • Yes, works nice, the game show ads when I press pause and hide them properly when resume, Thanks a lot EDIT: But adViews aren't good for performance, the game looks slow when I press pause button, looks like those constant ad loads burns my device – Santiago Cuartas Arango May 09 '14 at 00:31
  • Is it after `AdView` destroy ? – Libin May 09 '14 at 00:37
  • after loadAd and after destroy. I mean, pressing pause in any case slows the game – Santiago Cuartas Arango May 09 '14 at 00:53
  • After `loadAd` it is possible, since the frequent update on UI. But `destroy` should not make it slow. – Libin May 09 '14 at 00:57
  • Is there not any performance tips? I tried ads:loadAdOnCreate="true" and show me error during compilation – Santiago Cuartas Arango May 09 '14 at 01:06
  • I don't think so, since the `AdView` code is not open to the developers, at least if we had, we can try to understand the implementation., One suggestion would be to try changing your `Layouts`, since when `AdView` is updated it should not after other views in your `layout` – Libin May 09 '14 at 01:09
  • Guys, is it wise to do anything on AdView (or any destroyable object in general) after it was destroyed? You call adView.destroy() on pause and then adView.loadAds() when unpausing. I was thinking about calling adView.pause() instead of destroy(). This should stop requests I believe. What do you think? – Dzik May 22 '14 at 09:43
  • 1
    I checked Google API and it says: 'Destroy the AdView. This method should be called in the parent Activity's onDestroy() method. No other methods should be called on the AdView after destroy() is called.'. So the solution above is not right. I'll be trying adView.pause(). The only thing to remeber here is that activity onResume() also calls adView.resume() and that is something we would like to avoid when our adView is hidden. Additional boolean 'pause' flag required. – Dzik May 23 '14 at 11:03
1

You can count the ad opened methods call. and after it reached up to your limit you can make your ad view invisible.

    @Override
    public void onAdOpened() {
        // Code to be executed when an ad opens an overlay that
        // covers the screen.

        adClickedCount++;
        if (adClickedCount>2){
            mAdView.setVisibility(View.INVISIBLE);

        }
AlixaProDev
  • 472
  • 1
  • 5
  • 13