How can I re-upload after I close the Admob 2021 interstitial?

Viewed 372

I'm switching to the new Admob 2021 interstitials. New InterstitialAd My main event has 10 episodes and no matter which one I switch to, an interstitial ad will appear. However, when I got back to the event, I noticed that it wasn't showing again. What I want to do is reload after closing the interstitial ad.

public class MainActivity extends Activity {

private InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  MobileAds.initialize(this, new OnInitializationCompleteListener() {
    @Override
    public void onInitializationComplete(InitializationStatus initializationStatus) {}
  });
  AdRequest adRequest = new AdRequest.Builder().build();

  InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
  @Override
  public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
    // The mInterstitialAd reference will be null until
    // an ad is loaded.
    mInterstitialAd = interstitialAd;
    Log.i(TAG, "onAdLoaded");
  }

  @Override
  public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
    // Handle the error
    Log.i(TAG, loadAdError.getMessage());
    mInterstitialAd = null;
  }
});

} }

1 Answers

If you can use hilt this is so simple just copy-paste this code its code work great in my production app

AdmobUtil.kt

@Singleton
class AdmobUtil @Inject constructor(@ApplicationContext private val context: Context) {

    companion object {
        private const val TEST_INTERSTITIAL_AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712"

        private const val INTERSTITIAL_AD_UNIT_ID = ""
    }

    private lateinit var interstitialAd: InterstitialAd

    init {
        MobileAds.initialize(context) {
            loadAdmobInterstitialAd()
        }
    }

    private val interstitialAdUnitId = when {
        (BuildConfig.DEBUG) -> TEST_INTERSTITIAL_AD_UNIT_ID
        else -> INTERSTITIAL_AD_UNIT_ID
    }

    private fun loadAdmobInterstitialAd() {

        interstitialAd = InterstitialAd(context)

        interstitialAd.adUnitId = interstitialAdUnitId

        interstitialAd.loadAd(AdRequest.Builder().build())

        interstitialAd.adListener = object : AdListener() {
            override fun onAdClosed() {
                super.onAdClosed()
                reloadInterstitialAd()
            }

            override fun onAdFailedToLoad(error: LoadAdError?) {
                super.onAdFailedToLoad(error)
            }
        }
    }

    fun reloadInterstitialAd() {
        if (::interstitialAd.isInitialized) interstitialAd.loadAd(AdRequest.Builder().build())
    }

    fun showInterstitialAd() {
        if (::interstitialAd.isInitialized && interstitialAd.isLoaded) interstitialAd.show()
    }

}

MainApplication.kt

@HiltAndroidApp
class MainApplication : Application() {

    @Inject
    lateinit var admobUtil: AdmobUtil

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        admobUtil.reloadInterstitialAd()
    }
}

MainAcitivty.kt

@AndroidEntryPoint
class MainActivity : AppCompatActivity(R.layout.main_activity) {

    @Inject
    lateinit var admobUtil: AdmobUtil

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        admobUtil.showInterstitialAd()
    }

}
Related