show Interstitial Ad on back button pressed?

问题: This is my manifest file: <activity android:screenOrientation="portrait" android:name="com.google.android.gms.ads.AdActivity" android:configCha...

问题:

This is my manifest file:

<activity
        android:screenOrientation="portrait"
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

This is my activity:

 @Override
public void onBackPressed() {
    InterstitialAd interstitialAd= new InterstitialAd(this);
    interstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
    AdRequest adRequest = new AdRequest.Builder().build();
    interstitialAd.loadAd(adRequest);

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {

            if (interstitialAd.isLoaded()) {
                interstitialAd.show();
            }

        }

        @Override
        public void onAdClosed() {
            super.onAdClosed();
            finish();

        }
    });

}

I am getting an ad screen only 2-3 times while pressing the back button.So,how can I solve it, on every back button press.


回答1:

well try this

InterstitialAd interstitialAd = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blue);

       interstitialAd= new InterstitialAd(this);
        interstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
        AdRequest adRequest = new AdRequest.Builder().build();
        interstitialAd.loadAd(adRequest);

    }


    @Override
    public void onBackPressed() {
    if (interstitialAd.isLoaded()) {
        interstitialAd.show();
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                finish();
            }
        });
       }else{
           super.onBackPressed();
       }

    }

回答2:

if you added the same code in onStart/onResume remove it or remove code in onBackPressed.

Because When ActivityB takes the foreground, Activity A in onPause and onStop state.

When ActivityA comes back(when Back pressed from activity B), it will get onStart, then it call onResume(Activity A).


回答3:

Problem with your code is you are keeping interstetial ads to load and show both on onback press, loading interstetial add will take time and some times it also fails to load , when it fails to load ads or if no ads are available in admob ads wont be shown

better thing is to load interstitial ads in oncreate method and show them on on back as max suggested in his answer

and also in your code how will user exit from screen is ad is not available?

that case is also covered in max answer so go with his answer . In case ad is not shown you have to consider that ad is not present its not a fault in your code , i will also suggest to use this id while testing it is test ad and its hit rate will be very high

interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
  • 发表于 2019-07-07 02:40
  • 阅读 ( 483 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除