6

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.

Ahmed Alnabhan
  • 608
  • 1
  • 9
  • 13
ali abbas
  • 93
  • 1
  • 1
  • 6

5 Answers5

23

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();
}
Tim
  • 41,901
  • 18
  • 127
  • 145
  • thanks Tim.. but In Activity B i have to create and initialize "admanager" and then i wont be able to call getAd(); – ali abbas Nov 10 '14 at 20:42
  • You can circumvent that by making the Ad a static, see my edit – Tim Nov 10 '14 at 20:44
  • 1
    Hi Tim.. Your code doesnt work.. The constructor is undefined.. please fix that. – Arief Rivai May 29 '15 at 15:56
  • @AriefRivai which constructor – Tim May 29 '15 at 15:59
  • Hi Tim, hope you are still looking at this. The code above is not complete. I am not able to add the line interstitialAd = new InterstitialAd(this); without having the class "extend context". If I make the class extend context, then I also am forced to make it abstract and then we run into all kinds of issues. What can I substitute for "this" in the problem line, nothing I am trying is working. Thank you. – Bisclavret Jun 21 '15 at 06:08
  • @Bisclavet try to replace this with getapplicationcontext() – Tim Jun 21 '15 at 10:12
  • Working fine if class using extends Activity and send context from Activity A to admanager. Thanks Tim! – Arief Rivai Feb 14 '16 at 09:26
  • For people asking, just pass context to the constructor and from there to the createAd – Snake Apr 27 '16 at 22:50
  • Shouldn't ad be created with context in which it would be shown? – YTerle Aug 09 '16 at 14:19
  • @YTerle sure, but this is pseude code, so you should cater to that yourself – Tim Aug 09 '16 at 14:25
  • Thank you very much. It was very useful to me. – Xingxing Apr 19 '20 at 14:45
5

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();
    }
3

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.

Mark
  • 7,446
  • 5
  • 55
  • 75
2

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();
}
gurkan stack
  • 413
  • 1
  • 15
  • 43
0

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.

luckybilly
  • 221
  • 1
  • 9