I want to make sure that interstitial ads on Android, using the Android AdMob SDK, can be closed. After some research, it seems to me that this is not possible due to the ad being a separate activity. What I want to do is close the ad after 5 seconds. Can someone assure me that closing an interstitial ad is impossible or if there's a hack?
Asked
Active
Viewed 1.1k times
4 Answers
15
It's going to be a very late answer but I faced similar issue. A trick can be to call the back button event programmatically.
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
As whenever you press on back button the interstitial ad is closed so firing the back button event will eventually close the interstitial ad. Rest depends on the requirement.

gprathour
- 14,813
- 5
- 66
- 90
-
could you share more. details on how to do this. – Briantical Apr 19 '21 at 19:45
-
It didn't worked out! – Apoorv Pandey Sep 13 '22 at 19:35
4
You can get a reference of the Admob Interstitial activity and call finish()
on it when you want to close it, by implementing ActivityLifecycleCallbacks
:
private final Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
Log.e(TAG, "onActivityCreated: " + activity.getClass());
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
Log.e(TAG, "onActivityStarted: " + activity.getClass());
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
Log.e(TAG, "onActivityResumed: " + activity.getClass());
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
Log.e(TAG, "onActivityPaused: " + activity.getClass());
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
Log.e(TAG, "onActivityStopped: " + activity.getClass());
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
Log.e(TAG, "onActivitySaveInstanceState: " + activity.getClass());
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
Log.e(TAG, "onActivityDestroyed: " + activity.getClass());
}
};
MyApplication.getInstance().registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
E/TAG: onActivityCreated: class com.google.android.gms.ads.AdActivity
E/TAG: onActivityStarted: class com.google.android.gms.ads.AdActivity
E/TAG: onActivityResumed: class com.google.android.gms.ads.AdActivity

Goran Horia Mihail
- 3,536
- 2
- 29
- 40
0
For new readers. use:
Intent intent = new Intent(activity, activity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
Log.d("mInterstitialAd", "onAdOpened ");
fullscreenAdShowing = true;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if(!fullscreenAdShowing){
return;
}
Log.d("mInterstitialAd", "onAdOpened Handler 5 seconds run");
Intent intent = new Intent(activity, activity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
}
}, 5000);
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
Log.d("mInterstitialAd", "onAdClosed loadAd");
fullscreenAdShowing = false;
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}

NBApps
- 511
- 5
- 12