How To preload AdMob interstitial ad and send to another android activity using intent

13.5k views Asked by At

I need some help regarding AdMob interstitial ad.

I want to preload the interstitial ad in one activity. this is straight forward.

// Create an ad.
        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(TEST_DEVICE_ID).build();

        // Load the interstitial ad.
        interstitialAd.loadAd(adRequest);

Now I want to send the interstitial Ad to another activity using intent. I don't know how to send it using

intent.putExtra("myAd", interstitialAd);

Thanks in advance.

5

There are 5 answers

11
Tim On BEST ANSWER

Interstitial ads are not meant or built to be passed around like that using intents' extras.

It's better to

  • recreate & reload an ad in the next activity
  • make an extra public class that holds the interstitial ad, put it there in activity A and retrieve it from there in activity B

Example for 2nd case (semi pseudo code):

public class AdManager {
    // Static fields are shared between all instances.
    static InterstitialAd ad;
    private Context ctx;

    public AdManager(Context ctx) {
        this.ctx = ctx;
        createAd();
    }

    public void createAd() {
        // Create an ad.
        ad = new InterstitialAd(ctx);
        ad.setAdUnitId(AD_UNIT_ID);

        final AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(TEST_DEVICE_ID).build();

        // Load the interstitial ad.
        ad.loadAd(adRequest);
    }

    public InterstitialAd getAd() {
        return ad;
    }
}

Using

Activity A

AdManager adManager = new AdManager();
adManager.createAd();

Activity B

AdManager adManager = new AdManager();
InterstitialAd ad = adManager.getAd();
if (ad.isLoaded()) {
    ad.show();
}
0
luckybilly On

In ThisActivity, you can pre-load ad object with PreLoader asynchronously:

int preLoaderId = PreLoader.preLoad(new Loader());
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("preLoaderId", preLoaderId);
startActivity(intent);

//DataLoader, pre-load ad object
class Loader implements DataLoader<InterstitialAd> {
    @Override
    public InterstitialAd loadData() {

        // Create an ad object.
        InterstitialAd interstitialAd = new InterstitialAd(ThisActivity.this);
        interstitialAd.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(TEST_DEVICE_ID).build();

        // Load the interstitial ad.
        interstitialAd.loadAd(adRequest);
        return interstitialAd;
    }
}

in OtherActivity, you can get ad object by:

PreLoader.listenData(preLoaderId, new Listener());

//after ad load completed,DataListener.onDataArrived(...) will be called to process data
class Listener implements DataListener<InterstitialAd> {
    @Override
    public void onDataArrived(InterstitialAd ad) {
        //do sth with ad object
        //destroy this PreLoader by id if you don`t need it anymore
        PreLoader.destroy(preLoaderId);
    }
}

wish this is helpful.

1
Kirk Patrick Brown On

Solved this problem by using a singleton design pattern.

/**
* Created by Kirk on 10/26/2017.
*/

public class AdManager {

private static AdManager singleton;

public AdManager() {
}

/***
 * returns an instance of this class. if singleton is null create an instance
 * else return  the current instance
 * @return
 */
public static AdManager getInstance() {
    if (singleton == null) {
        singleton = new AdManager();
    }

    return singleton;
}

/***
 * Create an interstitial ad
 * @param context
 */
public static void createAd(Context context) {
    interstitialAd = new InterstitialAd(context);
    interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    interstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}

/***
 * get an interstitial Ad
 * @return
 */
public static InterstitialAd getAd() {
    return interstitialAd;
}

}

in activityA create an load ad

    AdManager adManager = AdManager.getInstance();
    adManager.createAd(MainActivity.this);

in Activity B retreive and show ad by

    AdManager adManager = AdManager.getInstance();
    InterstitialAd ad =  adManager.getAd();

    if (ad.isLoaded()) {
        ad.show();
    }
0
Mark On

InterstitialAd can be instantiated using any Context so you could instantiate it (and request ad) during onCreate of Application subclass and then later show the ad from anywhere in your code.

This way, you maximise the chances of an ad being loaded by the time you want to show it.

It would be good practice to use an AdManager (like mentioned in another answer), but not necessary to do so.

0
gurkan stack On

For people who tried Tim's answer and no success!

Do not create ads two times! Change constructor to

    public AdManager(Context ctx) {
        this.ctx = ctx;
        //createAd(); //Remove this line! 
    }

Finally, AdManager class should be like this:

public class AdManager {
    // Static fields are shared between all instances.
    static InterstitialAd ad;
    private Context ctx;

    public AdManager(Context ctx) {
        this.ctx = ctx;
       // createAd();
    }

    public void createAd() {
        // Create an ad.
        ad = new InterstitialAd(ctx);
        ad.setAdUnitId(AD_UNIT_ID);

        final AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(TEST_DEVICE_ID).build();

        // Load the interstitial ad.
        ad.loadAd(adRequest);
    }

    public InterstitialAd getAd() {
        return ad;
    }
}

Using

Activity A

AdManager adManager = new AdManager();
adManager.createAd();

Activity B

AdManager adManager = new AdManager();
InterstitialAd ad = adManager.getAd();
if (ad.isLoaded()) {
    ad.show();
}