Not shown in Fragment

1

I'm trying to show the interstitial on a fragment page but it does not play, this is the code I use:

    private InterstitialAd interstitial;
public void displayInterstitial() {
    if (interstitial.isLoaded()) {
        interstitial.show();
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.layout_recent_wallpaper, container, false);

    // Buscar AdView como recurso y cargar una solicitud.
    AdView adView = (AdView) rootView.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    // Crear el intersticial.
    interstitial = new InterstitialAd(getActivity());
    interstitial.setAdUnitId("ID_ADMOB");

    // Crear la solicitud de anuncio.
    adRequest = new AdRequest.Builder().build();

    // Comenzar la carga del intersticial.
    interstitial.loadAd(adRequest);

grid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                displayInterstitial();

            Intent intslider = new Intent(getActivity(), ActivitySlideImage.class);
            intslider.putExtra("POSITION_ID", position);
            intslider.putExtra("IMAGE_ARRAY", allArrayImage);
            intslider.putExtra("IMAGE_CATNAME", allArrayImageCatName);

            startActivity(intslider);

        }

    });

return rootView;
}

Does anyone know why the ad is not showing ??

    
asked by Hugo AE 12.12.2016 в 14:47
source

1 answer

1

It is important to add a true Ad Unit or the one displayed in the LogCat:

interstitial.setAdUnitId("ID_ADMOB");

In your code the ad load is related to clicking on the Grid, which I do not see fit:

grid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                displayInterstitial(); //Este método trata de cargar el anuncio.
                ...
                ...

It must be taken into account that the creation of the Fragment takes a little time, for this reason it would be good to add a listener and if the ad is not loaded, force the loading:

 interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if (interstitial.isLoaded()) 
            interstitial.show();
        }
        @Override
        public void onAdClosed() {
            super.onAdClosed();
            //interstitial.loadAd(request.build());
        }
    });

    // Comenzar la carga del intersticial.
    interstitial.loadAd(adRequest);
    
answered by 12.12.2016 в 16:57