Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid interstitial ad to reload after closing it

When I try to close Ad by clicking the close button in the interstitial ad, that particular ad is closed. But then another one ad is loaded. And the application continues in this way forever.

How to avoid another ad to load after manually closing it?

This is my code:

static View setupListView(final Activity activity, View convertView, final ViewGroup parent,
                              MediaBrowserCompat.MediaItem item) {

        if (sColorStateNotPlaying == null || sColorStatePlaying == null)
            initializeColorStateLists(activity);


        MediaDescriptionCompat description = item.getDescription();
        final MediaItemViewHolder holder;
        Integer cachedState = STATE_INVALID;

        holder = new MediaItemViewHolder();



        if (MediaIDHelper.ADVERTISEMENT.equals(description.getMediaId())) {
            // Advert show
            convertView = LayoutInflater.
                from(activity).
                inflate(R.layout.fragment_list_ad, parent, false);




            try {
                MobileAds.initialize(activity, activity.getString(R.string.admob_app_id));
                holder.mAdView = convertView.findViewById(R.id.itemAd);
                adRequest = new AdRequest.Builder().build();
                holder.mAdView.loadAd(adRequest);
            } catch (Exception ex) {
                Log.e(TAG, ex.getMessage());
            }
            return convertView;
        }
        else if (MediaIDHelper.isItemHeader(description.getMediaId())) {
            convertView = LayoutInflater.
                from(activity).
                inflate(R.layout.fragment_list_header, parent, false);
        }
        else if (MediaIDHelper.isEBookHeader(description.getMediaId())) {


            convertView = LayoutInflater.
                from(activity).
                inflate(R.layout.fragment_ebook_header, parent, false);

            MobileAds.initialize(activity, activity.getString(R.string.admob_app_id));
            AdRequest adIRequest = new AdRequest.Builder().build();

            holder.interstitial = new InterstitialAd(activity);
            holder.interstitial.setAdUnitId(activity.getString(R.string.admob_interstitial_id));

            // Interstitial Ad load Request
            holder.interstitial.loadAd(adIRequest);

            // Prepare an Interstitial Ad Listener
            holder.interstitial.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    // Call displayInterstitial() function when the Ad loads
                    holder.displayInterstitial();
                }
            });
        }
        else if (
            MediaIDHelper.isBrowseable(description.getMediaId())
            && (
                MediaIDHelper.isEBook(description.getMediaId())) ||
                MediaIDHelper.MEDIA_ID_BY_QUEUE.equals(description.getMediaId())
            ) {
            convertView = LayoutInflater.
                from(activity).
                inflate(R.layout.fragment_ebook_item, parent, false);
        }
        else {
            // Everything else
            convertView = LayoutInflater.
                from(activity).
                inflate(R.layout.fragment_list_item, parent, false);
        }
        convertView.setTag(holder);

        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);

        if (holder.mTitleView != null) {
            holder.mTitleView.setText(description.getTitle());
        }

        if (holder.mDescriptionView != null) {
            holder.mDescriptionView.setText(description.getSubtitle());
        }


        if (holder.mImageView != null) {
            int state = getMediaItemState(activity, item);
            if (cachedState == null || cachedState != state) {
                if (MediaIDHelper.isBrowseable(description.getMediaId())
                    || MediaIDHelper.isEBookHeader(description.getMediaId()) ) {

                    Uri imageUri = item.getDescription().getIconUri();
                    GlideApp.
                        with(activity).
                        load(imageUri).
                        override(Target.SIZE_ORIGINAL).
                        fallback(R.drawable.default_book_cover).
                        error(R.drawable.default_book_cover).
                        /*listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                return false;
                            }
                        }).*/
                        into(holder.mImageView);

and displayInterstitial() method is

    public void displayInterstitial()
    {
        // If Interstitial Ads are loaded then show else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
like image 528
midou Avatar asked Oct 15 '22 13:10

midou


1 Answers

You put the ad in the wrong place, Put it in a fragment or Activity will work without problems

like image 58
waseel almahri Avatar answered Oct 31 '22 21:10

waseel almahri