flutter admob banner ad not reloading after opening

617

onApplicationExit is called in the process of leaving the application.
And since you dispose the ad in this moment, it won't be there when you return to the app.

Share:
617
Niek
Author by

Niek

Updated on December 28, 2022

Comments

  • Niek
    Niek over 1 year

    In my flutter project, using flutter BloC, I've started using admob ads using the google_mobile_ads package. Displaying of the banner ad is working fine. however, after you've clicked the ad and then return back to the app, the ad has disappeared. And when the ad eventually gets refreshed it is no longer clickable.

    The desired outcome would be that the ad either remains after navigating, or that a new one gets loaded when returning to the app.

    When returning to the app it logs: W/Ads (27214): #004 The webview is destroyed. Ignoring action.

    This is most likely from the adListener callbacks. The only one firing is the onAdOpened callback. onAdClosed never gets called. When returning to the app the ads isLoaded method always returns true.

    So far I've tried

    • Disposing the ad and then loading it again
    • Creating a completely new ad when rebuilding the page

    When searching for the webview is destroyed error I find answers like this where they manually destroy the webviews. I don't see how I would do this since I'm using the google mobile ads package and not using any sort of webview myself. The only method here would be dispose() on the ad object the package offers, which I've already tried.

    The ad I'm creating:

    BannerAd(
      adUnitId: 'ca-app-pub-3940256099942544/6300978111',
      request: AdRequest(),
      size: AdSize.largeBanner,
      listener: AdListener(
        onAdClosed: (ad) {
          print('banner ad closed');
        },
        onAdLoaded: (ad) => print('banner ad loaded'),
        onAdFailedToLoad: (ad, error) {
          print('banner ad failed');
        },
        onAdOpened: (ad) async {
          print('banner ad opened');
        },
        onApplicationExit: (ad) => ad.dispose(),
        onAppEvent: (ad, string1, string2) => print('ad app event: $string1, $string2'),
        onNativeAdClicked: (ad) => print('banner native ad clicked'),
      ),
    );
    

    The banner gets placed at the bottom of the page

    Scaffold(
      body: BlocBuilder<HomeBloc, HomeState>(
        builder: (context, state) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: [
              // rest of page content
              if (state.bannerAd != null)
                Container(
                  color: Colors.white,
                  width: size.width,
                  height: 100,
                  child: Center(
                    child: AdWidget(ad: state.bannerAd),
                  ),
                ),
            ],
          );
        },
      ),
    )
    

    I'd very much appreciate any advice you could give me on why the ad doesn't reload as expected.

    SOLUTION

    When opening the ad onApplicationExit gets called, disposing my ad. Simply removing the dispose call from the callback and moving it to the close method of my BloC (I'm using flutter BloC in my project) solves the problem and the ad remains loaded when navigating back to the app after opening.

  • Niek
    Niek about 3 years
    after looking into it that did indeed seem to be the problem as I tried disposing it on open and then reloading on close. I am now disposing the ad outside of its own listener