Este documento se ha actualizado.
La traducción en Español (España) no está disponible todavía.
Actualización del documento en inglés: 18 sep. 2020
Actualización del documento en Español (España): 26 nov. 2018

Audience Network SDK Integration Tips on Android

Here are some integration best practices to for developer implementation on Android. These coding tips and scenarios will help your team better understand our Audience Network SDK, make the integration process more effective, and keep your solution up to date after launch.

Prerequisites

Ensure you have completed one of those examples:

Issue: Crashes Caused by Calling loadAd Multiple Times.

Good Practices

Bad Practices


Good Practices:

Tip 1: Only call loadAd again when the ad has been dismissed or ad loading comes across an error, except for banner ads.

Even if you get notified of ad dismiss or error on the callback, ensure you have Max Number of Retries to avoid retrying indefinitely. The onError(Ad, AdError) will get called no matter what causes ad load failure. For example, in case of "No Internet", you are supposed to stop loading the ad when it reaches to Max Number of Retries.

In the Audience Network SDK, for any type of ad except for banner ad, you should be careful when you call loadAd. You should never call loadAd while an ad is displaying. The best practice is to implement AdListener to get notified whenever an ad has dismissed, completed, closed, or come across errors, which allows you to legally call loadAd again and does not cause any issues.

Example 1: Call loadAd in onInterstitialDismissed Callback.

private InterstitialAd mInterstitialAd;
private static final MAX_NUMBER_OF_RETRIES = 3;
private boolean shouldLoadAd = true;
private int retryCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onCreate 
    super.onCreate(savedInstanceState);
    ...
    mInterstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
    InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
        ...
        @Override
        public void onInterstitialDismissed(Ad ad) {
            // Interstitial dismissed callback
            if (shouldLoadAd) {
                /* Change shouldLoadAd value to false, 
                    or a new interstitial ad will show immediately 
                    when previous interstitial ad gets dismissed. */
                shouldLoadAd = false;
                mInterstitialAd.loadAd();
            }
        }
        
        @Override
        public void onError(Ad ad, AdError adError) {
            // Ad error callback
            // Stop retrying when it reaches to MAX_NUMBER_OF_RETRIES
            if(retryCount < MainActivity.MAX_NUMBER_OF_RETRIES) {
                retryCount += 1;
                mInterstitialAd.loadAd();
            }
        }
        ...
    };
    mInterstitialAd.loadAd(
            mInterstitialAd.buildLoadAdConfig()
                    .withAdListener(interstitialAdListener)
                    .build());
    ...
}

Example 2: Call loadAd in onError callback for NativeAd.

private static final MAX_NUMBER_OF_RETRIES = 3;
private NativeAd mNativeAd;
private int retryCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
    NativeAdListener nativeAdListener = new AdListener() {
        ...
        @Override
        public void onError(Ad ad, AdError error) {
            // Ad error callback
            // Stop retrying when it reaches to MAX_NUMBER_OF_RETRIES
            if (retryCount < MainActivity.MAX_NUMBER_OF_RETRIES) {
                retryCount += 1;
                mNativeAd.loadAd();
            }
        }
        ...
    };
    mNativeAd.loadAd(
            mNativeAd.buildLoadAdConfig()
                    .withAdListener(nativeAdListener)
                    .build());
    ...
}

Tip 2: Ad Instance is not an ad manager. You are supposed to instantiate a new instance whenever you need "reload" an ad for native ads and banner ads.

Example:

private Button showNativeAdButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    showNativeAdButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Create a native ad request with a placement ID
            nativeAd = new NativeAd(NativeAdSampleActivity.this, "YOUR_PLACEMENT_ID");

            // Create a listener to get notified when the ad was loaded.
            NativeAdListener nativeAdListener = new AdListener() {
                ...
            }

            // Initiate a request to load an ad.
            nativeAd.loadAd(
                    nativeAd.buildLoadAdConfig()
                            .withAdListener(nativeAdListener)
                            .build());
        }
    });
    ...
}

Bad Practices:

Tip 1: Never use loadAd in some Android callbacks, such as onResume() or onStart() callback, which may cause loadAd to be called over and over without dismissing or releasing the last ad in memory.


Example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
    ...
}

@Override
protected void onResume() {
    super.onResume();
    ...
    // DON'T DO THIS!
    mNativeAd.loadAd(
            mNativeAd.buildLoadAdConfig()
                    .withAdListener(NativeAdSampleActivity.this)
                    .build());
}

Tip 2: Tip 2: Be cautious to not call loadAd indefinitely in onError callback. Below is an example of what you should never do. In case of "No Internet", loadAd will be called over and over, which finally crashes your app.


Example:

private NativeAd mNativeAd;
private boolean shouldRetry = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
    NativeAdListener nativeAdListener = new AdListener() {
        ...
        @Override
        public void onError(Ad ad, AdError error) {
            // Ad error callback
            // Remember to change shouldRetry to false, or DON'T implement like this! 
            if (shouldRetry) {
                mNativeAd.loadAd();
            }
        }
        ...
    };
    mNativeAd.loadAd(
            mNativeAd.buildLoadAdConfig()
                    .withAdListener(NativeAdSampleActivity.this)
                    .build());
    ...
}

Next Steps

More Resources

Getting Started Guide

Technical guide to get started with Audience Network

API Reference

Facebook SDK for iOS Reference