I want to Show the Ad after i click the default back button in my activity?

348 views
Skip to first unread message

Rocky

unread,
Jun 7, 2016, 6:54:04 AM6/7/16
to Google Mobile Ads SDK Developers
I am having  activities ,i want to show interstitial ad after i click the back button in the title bar in the 3rd activity..
1)OnAdClosed it sould return to the 2nd activity...
2)Ad should be visible as soon as i click the back button

give me the code and where to place them..?
Do i put the code in OnOptionsItemSelected in R.id.home because when we click the back button in the title bar,it calls that method

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Jun 7, 2016, 11:08:32 AM6/7/16
to Google Mobile Ads SDK Developers
Hi Rocky,

It looks like you are having an Up navigation button in your Action Bar. In that case, if you want to show an Interstitial Ad before you go back to the previous Activity, then you must call InterstitialAdView.show() in the onOptionsItemSelected() method. Then in the onAdClosed(), simply navigate yourself back to old Activity.

For example:
In the onOptionsItemSelected() method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    } else {
        NavUtils.navigateUpFromSameTask(this);
    }
        return true;
    }
    return super.onOptionsItemSelected(item);
}
And then in the onAdClosed() method:
 @Override    
 public void onAdClosed() {
     NavUtils.navigateUpFromSameTask(this);
 }

Thanks,
Veer Busani
Mobile Ads SDK Team

Rocky

unread,
Jun 8, 2016, 12:17:15 AM6/8/16
to Google Mobile Ads SDK Developers
WHERE SHOULD I WRITE THE OnAdClosed method..
In the OnCreate?

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Jun 8, 2016, 11:58:29 AM6/8/16
to Google Mobile Ads SDK Developers
Hi Rocky,

You can look and analyze this sample code to understand how an Interstitial is loaded. When you create an new instance of an Interstitial object, you need to set an AdListener if you want to be notified of any Ad Events. So, you can have the onAdClosed() method, which is part of the AdListener, whenever you are creating a new instance of the Interstitial AdView.

Thanks,
Veer Busani
Mobile Ads SDK Team

Reply all
Reply to author
Forward
0 new messages