4

As of v5.6.1 the interstitialAd.setAdListener is deprecated. What should I replace deprecated Facebook InterstitialAd setAdListener now ?

3 Answers3

2

From https://developers.facebook.com/docs/audience-network/reference/android/com/facebook/ads/interstitialad.html/?version=v5.6.1 it says

Deprecated. since 5.6, use loadAd(InterstitialAd.InterstitialLoadAdConfig)

This config you can get by calling buildLoadAdConfig() and there you can do withAdListener(InterstitialAdListener adListener) on.

Ivo
  • 18,659
  • 2
  • 23
  • 35
2
 val interstitialAdFb = com.facebook.ads.InterstitialAd(mContext, resources.getString(R.string.fb_interstitial_id))
    val adListener = object : com.facebook.ads.InterstitialAdListener {
        override fun onLoggingImpression(p0: Ad?) {
            TODO("Not yet implemented")
        }
        override fun onInterstitialDisplayed(p0: Ad?) {
            TODO("Not yet implemented")
        }
        override fun onAdClicked(p0: Ad?) {
            TODO("Not yet implemented")
        }
        override fun onInterstitialDismissed(p0: Ad?) {

        }

        override fun onError(p0: Ad?, p1: AdError?) {

        }

        override fun onAdLoaded(p0: Ad?) {

        }
    }

    val loadAdConfig = interstitialAdFb.buildLoadAdConfig()
            .withAdListener(adListener)
            .build()

    interstitialAdFb.loadAd(loadAdConfig)

and when you want to show fb interstitial ad then

if (interstitialAdFb.isAdLoaded)
        interstitialAdFb.show()

this code for when using

implementation 'com.facebook.android:audience-network-sdk:5.6.1'
Abdur Rehman
  • 1,247
  • 10
  • 13
2

If anyone needs, I have attached the full code which is working for me:

//Add this line to your manifest, you can add this line in application tag for the whole app or in specific activity tag where you want to put Interstitial ad.

    android:hardwareAccelerated="true"

//Activity code
    private InterstitialAd interstitialAd;
    public static boolean isAdLoadedOnce = false;

//onCreate
interstitialAd = new InterstitialAd(this, "Placement_id");
        interstitialAd.loadAd(interstitialAd.buildLoadAdConfig()
        .withAdListener(new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                isAdLoadedOnce = true;
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {

            }

            @Override
            public void onError(Ad ad, AdError adError) {

            }

            @Override
            public void onAdLoaded(Ad ad) {

            }

            @Override
            public void onAdClicked(Ad ad) {

            }

            @Override
            public void onLoggingImpression(Ad ad) {

            }
        })
        .withCacheFlags(ALL)
        .build());

//I have shown the ad in onBackPressed(), you can show wherever you want
public void onBackPressed() {

        if (!isAdLoadedOnce && interstitialAd.isAdLoaded() && !interstitialAd.isAdInvalidated()) {
            interstitialAd.show();

        } else {
            super.onBackPressed();
        }
    }

//Also need to override this method
@Override
    protected void onDestroy() {
       if (interstitialAd != null) {
            interstitialAd.destroy();
        }
        super.onDestroy();
    }

Details: https://developers.facebook.com/docs/audience-network/changelog-android#5_6_0

Delowar Hossain
  • 687
  • 10
  • 18