Android Interstitial Example

In the example below, an interstitial ad is displayed when the menu item is clicked. When the ad status changes, the snackbar is displayed with the corresponding message.

In a real application, you could use FuseAdViewEvent.Dismissed, FuseAdViewEvent.DisplayTimeout or FuseAdViewEvent.DisplayError event to trigger navigation to another screen.

class MainActivity : AppCompatActivity() {
    private var interstitialAd = FuseFullScreenAdView(this, "interstitial").apply {
        listener = object : FuseFullScreenAdViewListener {
            override fun onEvent(
                adView: FuseFullScreenAdView,
                event: FuseAdViewEvent,
            ) {
                val msg = when (event) {
                    is FuseAdViewEvent.Dismissed -> "Interstitial ad was dismissed by the user"
                    is FuseAdViewEvent.DisplayTimeout -> "Interstitial ad display timeout"
                    is FuseAdViewEvent.DisplayError -> "Interstitial ad display error (${event.error.message})"
                    else -> null
                }
                msg?.let {
                    showSnackBar(it)
                }
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
        R.id.action_interstitial -> {
            interstitialAd.show(this)
            true
        }

        else -> super.onOptionsItemSelected(item)
    }

    private fun showSnackBar(message: String) {
        Snackbar
            .make(
                this,
                binding.root,
                message,
                Snackbar.LENGTH_SHORT,
            )
            .show()
    }
}