1

I'm having a strange issue where my AdMob AdView banner seems to disappear when the device is rotated. I initially solved this by setting android:configChanges="orientation|keyboardHidden|screenSize" on the activity in the manifest file. This worked fine until I made a second layout to use for landscape on tablets. The screenSize flag means that the phone will not dynamically show the landscape/portrait layout, it instead uses the same layout for the original orientation. Is there anyway that I can persist the AdView banners when rotating the screen without using this flag?

My xml is as follows

<com.google.android.gms.ads.doubleclick.PublisherAdView
 android:id="@+id/ad_view"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:visibility="gone"
 ads:adSize="MEDIUM_RECTANGLE"
 ads:adUnitId="@string/test_id"/>

The banner itself is loaded when the activity has received a response from the API for a network call, which is made when the fragment has been added to the activity.

@Override
protected void onResume() {
    super.onResume();
    if (mAdView != null) {
        mAdView.resume();
    }
}

@Override
protected void onPause() {
    if (mAdView != null) {
        mAdView.pause();
    }
    super.onPause();
}

@Override
protected void onDestroy() {
    if (mAdView != null) {             
        mAdView.destroy();
    }
    super.onDestroy();
}

    private void onNetworkCallSuccessful() {
        mAdView = (PublisherAdView) findViewById(R.id.ad_view);
        setupAdMobListener();
        Bundle bundle = new Bundle();
        bundle.putString("testKey","testValue");
        mAdView.loadAd(new PublisherAdRequest.Builder().addNetworkExtras(new AdMobExtras(bundle)).build();
}

Any advice would be greatly appreciated.

chuckliddell0
  • 2,061
  • 1
  • 19
  • 25
  • 1
    Do you have XML layout specifically for landscape in `res/layout-land`? – Code-Apprentice Feb 12 '16 at 17:03
  • Yes I do. The PublisherAdView xml above is the exact same as the landscape version. My landscape version has a two pane layout, for tablets. – chuckliddell0 Feb 12 '16 at 17:05
  • PublisherAdView visibility is set to Gone... Is that right? In the code above, you don't set it to visible again... Another point is, during screen rotation, the view is destroyed... is onNetworkCallSuccessful() called again after screen rotation? – guipivoto Feb 12 '16 at 17:39
  • It is set to gone. In my 'setupAdMobListener()' I add a new 'adListener' where I override 'onAdLoaded'. In onAdLoaded I then set the visibility to visible. No 'onNetworkCallSuccessful()' is not called again after the screen rotationm as I do not want to make another network call to load a new ad. I want to persist the ad that I have already loaded. – chuckliddell0 Feb 12 '16 at 17:49
  • 2
    I think it is not possible to remove android:configChanges and keep the same AD. If you remove configChanges, the AD will be destroyed during screen rotation. You must load a new one. So, I believe the option is keep android:configChanges and you should be responsible to handle screenRotation at your side (and no longer automatically by android). But even then, I'm not sure if you can keep the same AD.. – guipivoto Feb 12 '16 at 18:05

0 Answers0