googleads / googleads-mobile-android-examples

googleads-mobile-android
Apache License 2.0
1.67k stars 1.86k forks source link

[Urgent Error] BannerRecyclerView has Loading and Destroy Error! #641

Open theoyuncu8 opened 5 months ago

theoyuncu8 commented 5 months ago

I got this working for my recyclerview app but there is a problem. When the Activity opens, a banner ad is loaded every 8 items, and even if you destroy it in the onDestroy method, it does not close! This is a critical bug that needs to be resolved! So even if the activity is terminated, adView is reloaded. I tried such a method, but it is not correct because clearing the list and terminating the adapter while the elements are loading cannot be the right solution. Please update the method

dest

Link: https://github.com/googleads/googleads-mobile-android-examples/blob/master/java/advanced/BannerRecyclerViewExample/app/src/main/java/com/google/android/gms/example/bannerrecyclerviewexample/MainActivity.java


    @Override
    protected void onResume() {
        super.onResume();
        for (Object item : recyclerViewItems) {
            if (item instanceof AdView) {
                AdView adView = (AdView) item;
                adView.resume();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        for (Object item : recyclerViewItems) {
            if (item instanceof AdView) {
                AdView adView = (AdView) item;
                adView.pause();
                Log.e("MainActivity", "PAUSE Banner Ad");
            }
        }
    }

    @Override
    protected void onDestroy() {
        for (Object item : recyclerViewItems) {
            if (item instanceof AdView) {
                AdView adView = (AdView) item;
                adView.destroy();
                Log.e("MainActivity", "DESTROY Banner Ad");
            }
        }
        super.onDestroy();
    }

    private void loadBannerAd(final int index) {
        if (index >= recyclerViewItems.size()) {
            return;
        }

        Object item = recyclerViewItems.get(index);
        if (!(item instanceof AdView adView)) {
            throw new ClassCastException("Expected item at index " + index + " to be a banner ad" + " ad.");
        }

        adView.setAdListener(
                new AdListener() {
                    @Override
                    public void onAdLoaded() {
                        super.onAdLoaded();
                        Log.d("MainActivity", "Loaded Banner Ad");
                        loadBannerAd(index + ITEMS_PER_AD);
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        Log.d("MainActivity", "Failed Banner Ad");
                        loadBannerAd(index + ITEMS_PER_AD);
                    }
                });

        adView.loadAd(new AdRequest.Builder().build());
    }
theoyuncu8 commented 4 months ago

This error still persists. For example, if the size of the entire list is 1000 and an ad needs to be loaded in one of every 10 items, it is necessary to wait approximately 1 minute. However, if the user exits the activity while the ads are loading, the ads will continue to load. Please resolve this critical issue immediately!

NVentimiglia commented 3 months ago

Chain https://github.com/googleads/googleads-mobile-android-examples/issues/646