How to show Interstitial ad correctly in this case

2019-01-20 19:14发布

问题:

I created a soundboard with a gridview Adapter (so no chance to set the Interstitial Ad on a sound button) I also have a navigationbar with three tabs, so I decidet to set the Interstitial ad on the second fragment like this:

public class SecondFragment extends Fragment {
    private InterstitialAd mInterstitialAd;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.second_layout,container,false);

        final AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd = new InterstitialAd(getActivity());
        mInterstitialAd.setAdUnitId("MYID");
        mInterstitialAd.loadAd(adRequest);
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                }

            }

            @Override
            public void onAdClosed() {
            }
        });


        return rootView;
    }

But then the problem was that the Ad pops up with a 3 second delay, thats why I received a mail from AdMob that I have to set the Interstitial ad correctly and not like I did before.

So my question is, what would you do in this case? Where would you set the InterstitialAd?

This is my App, 3 fragments full with sound buttons: https://gyazo.com/1ecd359b38fcfe0606bb3e74b684f16e

回答1:

From your code you're giving request to load ad then when your ad is loaded then you show that ad.

There's always a delay in loading ads so my suggestion is load your ad previously is background and not show that ad. whenever you want to show ad only check is your interstitial is ready to show, if yes show.

Call createInterstitial() at once in background and after that whenever you want to show interstitial Ad, call showInterstitial().

public void crateInterstitial(){

    interstitialAd = new InterstitialAd(this.activity);
    interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // not call show interstitial ad from here            
        }

        @Override
        public void onAdClosed() {
              loadInterstitial(); 
         }
      });
    loadInterstitial();
}

public void loadInterstitial(){

     AdRequest interstitialRequest = new AdRequest.Builder().build();
     interstitialAd.loadAd(interstitialRequest);
}

public void showInterstitial(){
     if (interstitialAd.isLoaded()) 
          interstitialAd.show();                          
     else 
         loadInterstitial();                                                      
}