Reputation: 21
I have a simple game made in android sdk. When the user has level failed I want an interstitial to appear, more or less exactly at that moment. The problem is from the code I have, is a @5 seconds delay from the moment that interstitial function start until interstitial appear(checked in LogCat. Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) {
// Create the interstitial
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));
// Create ad request.
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
}
//initialise banner ad
this.BANNER_AD_UNIT_ID = getResources().getString(R.string.BannerAd_unit_id);
showBanner();
}
public void openAd() {
if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) {
runOnUiThread(new Runnable() {
public void run() {
if (!interstitial.isLoaded()) {
interstitial.loadAd(adRequest);
}
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
interstitial.show();
}
});
}
});
}
}
Is there any way to cache the interstitial before that function is called. That delay is really bad.... Thanks!
Upvotes: 0
Views: 1962
Reputation: 1
To solve the issue of delay actions after an interstitial ad is dismissed without relying on AdMob callbacks, you can simply use a flag to track the ad state.
Approach:
Set a flag to true when you show the interstitial ad. In the onResume() method of your activity, check if the flag is true. If it is, perform the desired action (like navigating or popping back). This approach avoids the need to directly handle ad callbacks like onAdDismissedFullScreenContent() and ensures smooth transitions when the ad is dismissed.
Explanation:
When an interstitial ad is shown, your activity goes into the paused state. After the ad is dismissed, your activity is resumed. By setting a flag when the ad is shown and checking that flag in onResume(), you can trigger the necessary actions quickly without any delay(like navigating or popping back).
class MainActivity : AppCompatActivity() {
private var isAdShown = false
override fun onResume() {
super.onResume()
if (isAdShown) {
// Perform the action you want after the ad is dismissed, e.g., navigate or pop the fragment
navigateToNextFragment()
isAdShown = false
}
}
private fun showInterstitialAd() {
// Assuming you've already loaded the ad
isAdShown = true
interstitialAd?.show(this)
}
private fun navigateToNextFragment() {
// Your navigation logic here
}
}
Upvotes: 0
Reputation: 20196
Your problem is that you are attempting to load the ad and show it at the same time. Loading requires a network request which is slow. This is never going to give you the experience you are after.
Recommended practice is to call interstitial.loadAd(adRequest)
at the start of your game or level. And then at the end of your level call
if (interstitial.isAdLoaded() {
interstitial.show();
}
Upvotes: 3