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.
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 {
@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 {
@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.