Interstitial ads

A full-screen ad format, interstitial ads are embedded in the app content at natural breaks, such as when a user advances to the next game level or completes a conversion action.

Note

Examples showing how all the format types work are available in the demo project.

Entity

Description

HandleAdFailedToLoad

If HandleAdFailedToLoad()returns an error, don't attempt to load a new ad again using the same method.

adUnitId

Use:

  • Development mode to work with demo ad units.

  • Production mode to work with R-M-XXXXXX-Y (for the actual ID, check the Yandex Advertising Network interface). R-M-XXXXXX-Y is a template for your actual ad unit ID that will be used to receive various creatives.

Example of creating an interstitial ad

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:yandex_mobileads/mobile_ads.dart';

class InterstitialPage extends StatefulWidget {
  const InterstitialPage({super.key});

  @override
  State<InterstitialPage> createState() => _InterstitialPageState();
}

class _InterstitialPageState extends State<InterstitialPage> {
  static const _tag = 'Interstitial';
  static const _adUnitId = 'demo-interstitial-yandex';
  InterstitialAdLoader? _loader;
  InterstitialAd? _ad;
  String _status = 'Initializing...';

  @override
  void initState() {
    super.initState();
    _init();
  }

  @override
  void dispose() {
    _ad?.destroy();
    super.dispose();
  }

  Future<void> _init() async {
    setState(() => _status = 'Creating loader...');

    _loader = await InterstitialAdLoader.create(
      onAdLoaded: (ad) {
        debugPrint('[$_tag] onAdLoaded');
        if (!mounted) { ad.destroy(); return; }
        _ad = ad;
        setState(() => _status = 'Showing...');
        _show();
      },
      onAdFailedToLoad: (error) {
        debugPrint('[$_tag] onAdFailedToLoad: ${error.description}');
        if (mounted) setState(() => _status = 'Error: ${error.description}');
      },
    );

    setState(() => _status = 'Loading ad...');

    await _loader!.loadAd(
      adRequestConfiguration: AdRequestConfiguration(adUnitId: _adUnitId),
    );
  }

  Future<void> _show() async {
    final ad = _ad;
    if (ad == null) return;

    ad.setAdEventListener(
      eventListener: InterstitialAdEventListener(
        onAdShown: () => debugPrint('[$_tag] onAdShown'),
        onAdDismissed: () {
          debugPrint('[$_tag] onAdDismissed');
          if (mounted) {
            setState(() => _status = 'Closed');
            Navigator.of(context).pop();
          }
        },
        onAdClicked: () => debugPrint('[$_tag] onAdClicked'),
        onAdFailedToShow: (error) {
          debugPrint('[$_tag] onAdFailedToShow: ${error.description}');
          if (mounted) setState(() => _status = 'Show error: ${error.description}');
        },
        onAdImpression: (ImpressionData impressionData) =>
            debugPrint('[$_tag] onAdImpression: $impressionData'),
      ),
    );

    await ad.show();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Interstitial')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const CircularProgressIndicator(),
            const SizedBox(height: 16),
            Text(_status),
          ],
        ),
      ),
    );
  }
}

Checking integration

Build and run your project. You can check if the integration is successful by searching the YandexAds keyword in Logcat in Android Studio:

[Integration] Ad type interstitial was integrated successfully