Getting Started
Interstitial ads are a type of ad format that occupies the entire screen of the app and is typically displayed naturally during moments when app usage is temporarily paused, such as during screen transitions within the app. This guide explains how to integrate interstitial ads using the NAM SDK.
Before You Begin
- Ad Unit ID is required to call ads.
- Please complete the process of setting up the ad provider, inventory settings, and registering ad units through the NAM Admin.
- For related details, please contact the NAM administrator.
- If there are other Views overlaying the ad, exposure measurement may not work properly in some cases, which could negatively impact performance metrics.
[Step 1] Complete NAM SDK Integration
Please refer to Getting Started.
The following assumes that the NAM SDK integration has been completed.
[Step 2] Initialize GfpInterstitialAdManager
To use interstitial ads, you need to initialize the GfpInterstitialAdManager. At this time, you must create an Ad Unit ID required for ad requests and an AdParam object containing runtime information (e.g., targeting information) for a single ad request, and pass them as parameters.
- Kotlin
- Java
class MainActivity : Activity() {
private var interstitialAdManager: GfpInterstitialAdManager? = null;
override fun onCreate(savedInstanceState: Bundle?) {
...
// Create a new ad parameter.
val adParam = AdParam.Builder()
.setAdUnitId("YOUR_AD_UNIT_ID")
...
.build()
// Create a new GfpInterstitialAdManager.
interstitialAdManager = GfpInterstitialAdManager(this, adParam)
}
}
public class MainActivity extends Activity {
private GfpInterstitialAdManager interstitialAdManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Create a new ad parameter.
AdParam adParam = new AdParam.Builder()
.setAdUnitId("YOUR_AD_UNIT_ID")
...
.build();
// Create a new GfpInterstitialAdManager.
interstitialAdManager = new GfpInterstitialAdManager(this, adParam);
}
}
[Step 3] Set InterstitialAdListener
The InterstitialAdListener is a listener that receives the load and display status of interstitial ads. It handles events related to the loading and display of GfpInterstitialAdManager. Before loading interstitial ads, you must set the InterstitialAdListener as shown below.
- Kotlin
- Java
interstitialAdManager.setAdListener(object: InterstitialAdListener() {
override fun onAdLoaded(ad: GfpInterstitialAd) {
// Called when an ad is loaded.
// You can show the ad here.
}
override fun onAdStarted(ad: GfpInterstitialAd) {
// Called when an ad is started or shown.
}
override fun onAdClicked(ad: GfpInterstitialAd) {
// Called when an ad is clicked.
}
override fun onAdClosed(ad: GfpInterstitialAd) {
// Called when an ad is closed.
}
override fun onError(ad: GfpInterstitialAd, error: GfpError) {
// Called when an error happened while the ad is
// attempting to load or rendering an ad.
}
}
interstitialAdManager.setAdListener(new InterstitialAdListener() {
@Override
public void onAdLoaded(GfpInterstitialAd ad) {
// Called when an ad is loaded.
// You can show the ad here.
}
@Override
public void onAdStarted(GfpInterstitialAd ad) {
// Called when an ad is started or shown.
}
@Override
public void onAdClicked(GfpInterstitialAd ad) {
// Called when an ad is clicked.
}
@Override
public void onAdClosed(GfpInterstitialAd ad) {
// Called when an ad is closed.
}
@Override
public void onError(GfpInterstitialAd ad, GfpError error) {
// Called when an error happened while the ad is
// attempting to load or rendering an ad.
}
});
[Step 4] Load an ad
You can load an ad by calling the loadAd() method of GfpInterstitialAdManager. If the ad is successfully loaded, the onAdLoaded() method of InterstitialAdListener will be called. If the ad fails to load, the onError() method will be called.
You need to load ads on the UI thread.
To reuse a previously declared GfpInterstitialAdManager for ad requests, you must call only loadAd() without calling destroy(). If you call destroy() before calling loadAd(), the request will occur with resources already released, which may result in issues such as not receiving ad events.
- Kotlin
- Java
class MainActivity : Activity() {
private var interstitialAdManager: GfpInterstitialAdManager? = null;
override fun onCreate(savedInstanceState: Bundle?) {
...
// Create a new ad parameter.
val adParam = AdParam.Builder()
.setAdUnitId("YOUR_AD_UNIT_ID")
...
.build()
// Create a new GfpInterstitialAdManager.
interstitialAdManager = GfpInterstitialAdManager(this, adParam).run {
// Set the ad listener
setAdListener(object: InterstitialAdListener() {
override fun onAdLoaded(ad: GfpInterstitialAd) {
// Called when an ad is loaded.
// You can show the ad here.
}
override fun onError(ad: GfpInterstitialAd, error: GfpError) {
// Called when an error happened while the ad is
// attempting to load or rendering an ad.
}
...
})
// Load the ad
loadAd()
}
}
}
public class MainActivity extends Activity {
private GfpInterstitialAdManager interstitialAdManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Create a new ad parameter.
AdParam adParam = new AdParam.Builder()
.setAdUnitId("YOUR_AD_UNIT_ID")
...
.build();
// Create a new GfpInterstitialAdManager.
interstitialAdManager = new GfpInterstitialAdManager(this, adParam);
// Set the ad listener
interstitialAdManager.setAdListener(new InterstitialAdListener() {
@Override
public void onAdLoaded(GfpInterstitialAd ad) {
// Called when an ad is loaded.
// You can show the ad here.
}
@Override
public void onError(GfpInterstitialAd ad, GfpError error) {
// Called when an error happened while the ad is
// attempting to load or rendering an ad.
}
...
});
// Load the ad
interstitialAdManager.loadAd();
}
}
[Step 5] Display an ad
When an ad is successfully loaded, the onAdLoaded method of InterstitialAdListener is called. At this point, you can use the GfpInterstitialAd object to display the ad. The integration method may vary depending on whether the interstitial ad is displayed immediately or after a certain period of time.
The GfpInterstitialAd parameter, which is commonly passed to all methods of InterstitialAdListener, is the same object as the GfpInterstitialAdManager used to load the ad.
1. Immediate Display
The following is an example of displaying a interstitial ad immediately after it is loaded.
- Kotlin
- Java
override fun onAdLoaded(ad: GfpInterstitialAd) {
// Interstitial ad is loaded and ready to be displayed
ad.show(this)
}
@Override
public void onAdLoaded(GfpInterstitialAd ad) {
// Interstitial ad is loaded and ready to be displayed
ad.show(this);
}
2. Display after a certain period of time
Here is a simple example of displaying an interstitial ad 10 minutes after it has been loaded. Please note that the code below is for reference purposes only.
Since interstitial ads have an expiration time, you must use the isAdInvalidated() method to check if the ad has expired when displaying a preloaded and cached ad. Displaying an expired ad will not grant rewards for ad exposure.
- Kotlin
- Java
class MainActivity : Activity() {
private var interstitialAdManager: GfpInterstitialAdManager? = null;
override fun onCreate(savedInstanceState: Bundle?) {
...
// Create a new GfpInterstitialAdManager.
interstitialAdManager = GfpInterstitialAdManager(this, adParam).run {
// Set the ad listener
setAdListener(object: InterstitialAdListener() {
override fun onAdLoaded(ad: GfpInterstitialAd) {
showAdWithDelay()
}
...
})
// Load the interstitial ad
loadAd()
}
}
private fun showAdWithDelay() {
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
interstitialAdManager?.run {
// Check if ad is already expired or invalidated,
// and do not show ad if that is the case.
// You will not get paid to show an invalidated ad.
if (!isAdInvalidated()) {
return
}
showAd(this@MainActivity)
}
}, 10 * 60 * 1000) // 10 minutes delay
}
}
public class MainActivity extends Activity {
private GfpInterstitialAdManager interstitialAdManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Create a new GfpInterstitialAdManager.
interstitialAdManager = new GfpInterstitialAdManager(this, adParam);
// Set the ad listener
interstitialAdManager.setAdListener(new InterstitialAdListener() {
@Override
public void onAdLoaded(GfpInterstitialAd ad) {
showAdWithDelay()
}
...
});
// Load the interstitial ad
interstitialAdManager.loadAd();
}
private void showAdWithDelay() {
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(() -> {
// Check if ad is already expired or invalidated,
// and do not show ad if that is the case.
// You will not get paid to show an invalidated ad.
if (interstitialAdManager.isAdInvalidated()) {
return;
}
interstitialAdManager.showAd(this);
}, 10 * 60 * 1000); // 10 minutes delay
}
}
[Step 6] Release Resources
After displaying an interstitial ad, you must use the destroy() method to release the resources allocated to the ad.
To reuse a previously declared GfpInterstitialAdManager for ad requests, you must call only loadAd() without calling destroy(). If you call destroy() before calling loadAd(), the request will occur with resources already released, which may result in issues such as not receiving ad events.
You must call destroy() for all rewarded ads, even if they are not used or referenced.
As shown in the example below, you should release all references to GfpInterstitialAdManager used within an Activity in the onDestroy() method of the Activity.
- Kotlin
- Java
override fun onDestroy() {
interstitialAdManager?.destroy()
interstitialAdManager = null
super.onDestroy()
}
@Override
public void onDestroy() {
if (interstitialAdManager != null) {
interstitialAdManager.destroy();
interstitialAdManager = null;
}
super.onDestroy();
}