0

i ve add a interstitial function on my app, it works great but the problem is that if user swype from portrait to landscape, the ad showing again. The ad showing causally and this breaks the google rules.

How i can set the interstitial Ad only at the open of the app? This is my simple code ad mob code

Admob code image

  • Such situation of orientation change, recreates you activity. You should handle this orientation change – jack jay Jun 12 '17 at 08:01
  • 1
    your activity should handle orientation change, not admob. Also showing interstitial ad on start of app is disallowed practice. Refer to this link https://support.google.com/admob/answer/6201362?hl=en – Amod Gokhale Jun 12 '17 at 09:24

3 Answers3

0

You can create a variable that count how many times the ad was displayed. It will look something like this:

Declaring the variable:

int counter = 0;

Displaying the interstitial ad:

  if (counter == 0 && intersitial.isLoaded()) {
    interstitial.show();
    counter++;
} else {
    AdRequest interstitialRequest = new AdRequest.Builder().build();
    interstitial.loadAd(interstitialRequest);
}

Beside this, you can try to add android:configChanges="orientation|screenSize" to your AndroidManifest.xml. This will prevent your app to restart when changing the orientation.

Moisoni Ioan
  • 1,370
  • 1
  • 13
  • 24
0

Try this -

Manifest file -

(All 3 parameters are required to handle orientation change)

android:configChanges="keyboardHidden|orientation|screenSize">

Activity file -

boolean isFirstTimeAdLoading = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotate);
        Log.e(TAG, "onCreate()");
        if(isFirstTimeAdLoading)
        {
            // load ad
            Log.e(TAG, "Ad Loaded for First Time");
            // TODO : Call your Ad Loading Method here


            // mark ad as 'already loaded once'
            isFirstTimeAdLoading = false;
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart()");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e(TAG, "onConfigurationChanged()");
        Log.e(TAG, "isFirstTimeAdLoading = " + isFirstTimeAdLoading);
    }

Logs -

06-12 14:44:15.900 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onCreate()
06-12 14:44:15.900 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: Ad Loaded for First Time
06-12 14:44:15.900 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onStart()
06-12 14:44:24.320 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onConfigurationChanged()
06-12 14:44:24.320 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: isFirstTimeAdLoading = false
06-12 14:44:26.070 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onConfigurationChanged()
06-12 14:44:26.070 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: isFirstTimeAdLoading = false
06-12 14:44:27.880 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onConfigurationChanged()
06-12 14:44:27.880 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: isFirstTimeAdLoading = false

If you see the Logs, you'll know that onCreate is called only once, and everytime the orientation changes after that, onConfigurationChanged() is called, and thus, Activity is not recreated everytime you rotate the screen (I rotated my device thrice after initial loading).

Kunal Chawla
  • 1,236
  • 2
  • 11
  • 24
0

Very simply, you should NOT be displaying the add in onAdLoaded. This is what is causing you pain. And it already violates Admob policy regardless of whether you rotate to landscape or not.

You should show the ad in a natural break point in your app.

William
  • 20,150
  • 8
  • 49
  • 91