Reputation: 25
Hello there i have a problem i've added admob interstitial to my application when i click a button but the problem is when i click the button the ad doesn't shows until i clicked back button on the phone then the ad shows
the app has 2 activities and the button take u to another activity and i want the ad to show-up before the second activity appears
this is the code
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
final Intent intent = new Intent(MainActivity.this, Main2Activity.class);
tip1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Tips tip = (Tips) DAOTips.lt.get(1);
intent.putExtra("tip",tip.getTip());
intent.putExtra("title",tip.getTitle());
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
startActivity(intent);
}
});
Upvotes: 1
Views: 642
Reputation: 164
The problem is that you are starting the ad but then immediately opening a new screen.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
startActivity(intent);
after show is called startActivity is run. You need to remove startActivity otherwise it will be the top activity on the stack and block the ad!
There are two ways,
1) show the ad on oncreate of activity2
2) use admobs listeners. admob has a listener that lets you know when the ad has closed. you can open the new activity once the ad has closed
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
}
@Override
public void onAdLoaded() {
}
@Override
public void onAdOpened() {
}
@Override
public void onAdClosed() {
//use this
}
});
Upvotes: 2