Flutter Google Admob (google_mobile_ads package) not disposed

225 views
Skip to first unread message

Batuhan Savaş Yılmaz

unread,
Sep 6, 2023, 4:38:07 AM9/6/23
to Google Mobile Ads SDK Developers
Hello from Turkey. I added admob ads to my Flutter application.There is an ad on each page in the 2-page application and there is such a situation that the ad on the main page stays open even if the page changes. But the ad on the 2nd page only opens when the page is changed. Is there a mistake here, is there a problem or does it work like this? I also examined the documentation and there is nothing missing in my code.


example code :

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  MobileAds.instance.initialize();
  runApp(const MaterialApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

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

  @override
  State<FirstRoute> createState() => _FirstRouteState();
}

class _FirstRouteState extends State<FirstRoute> {
  late BannerAd? _bannerAd;

  bool _isBannerAdReady = false;

  void _loadBannerAd() {
    _bannerAd = BannerAd(
      adUnitId: 'ca-app-pub-3940256099942544/6300978111',
      size: AdSize.banner,
      request: AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (_) {
          setState(() {
            _isBannerAdReady = true;
          });
        },
        onAdFailedToLoad: (ad, error) {
          _bannerAd!.dispose();
        },
      ),
    );

    _bannerAd!.load();
  }

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

  void dispose() {
    super.dispose();
    _bannerAd!.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Route'),
      ),
      body: Center(
        child: ElevatedButton(
          child: const Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const SecondRoute()),
            );
          },
        ),
      ),
      bottomNavigationBar: _isBannerAdReady
          ? SizedBox(
              width: _bannerAd!.size.width.toDouble(),
              height: _bannerAd!.size.height.toDouble(),
              child: AdWidget(ad: _bannerAd!))
          : SizedBox(
              width: _bannerAd!.size.width.toDouble(),
              height: _bannerAd!.size.height.toDouble(),
            ),
    );
  }
}

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

  @override
  State<SecondRoute> createState() => _SecondRouteState();
}

class _SecondRouteState extends State<SecondRoute> {
  late BannerAd? _bannerAd;

  bool _isBannerAdReady = false;

  void _loadBannerAd() {
    _bannerAd = BannerAd(
      adUnitId: 'ca-app-pub-3940256099942544/6300978111',
      size: AdSize.banner,
      request: AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (_) {
          setState(() {
            _isBannerAdReady = true;
          });
        },
        onAdFailedToLoad: (ad, error) {
          _bannerAd!.dispose();
        },
      ),
    );

    _bannerAd!.load();
  }

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

  void dispose() {
    super.dispose();
    _bannerAd!.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Route'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: const Text('Go back!'),
        ),
      ),
      bottomNavigationBar: _isBannerAdReady
          ? SizedBox(
              width: _bannerAd!.size.width.toDouble(),
              height: _bannerAd!.size.height.toDouble(),
              child: AdWidget(ad: _bannerAd!))
          : SizedBox(
              width: _bannerAd!.size.width.toDouble(),
              height: _bannerAd!.size.height.toDouble(),
            ),
    );
  }
}

Mobile Ads SDK Forum Advisor

unread,
Sep 6, 2023, 10:14:27 AM9/6/23
to batuhansa...@gmail.com, google-adm...@googlegroups.com
Hello,

Thank you for reaching out to us.

With regard to your concern, you will need to call the dispose() method when navigating way from 1st page to the next page.

 
This message is in relation to case "ref:_00D1U1174p._5004Q2oikv7:ref"

Thanks,
 
Google Logo Mobile Ads SDK Team


Batuhan Savaş Yılmaz

unread,
Sep 6, 2023, 10:30:09 AM9/6/23
to Google Mobile Ads SDK Developers
1.

          child: const Text('Open route'),
          onPressed: () {
setState((){
dispose();
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const SecondRoute()),
            );});
          },
        )
2.
          child: const Text('Open route'),
          onPressed: () {

            Navigator.push(context, MaterialPageRoute(builder: (context) => HedefSayfa())).then((result) {

_bannerAd!.dispose();
});

          },
        )


Neither of them are working.

6 Eylül 2023 Çarşamba tarihinde saat 17:14:27 UTC+3 itibarıyla Mobile Ads SDK Forum Advisor şunları yazdı:

Mobile Ads SDK Forum Advisor

unread,
Sep 6, 2023, 4:53:21 PM9/6/23
to batuhansa...@gmail.com, google-adm...@googlegroups.com
Hello,

Upon reviewing the screen recording you provided, it appears that there is no issue and this is the intended behavior. The banner ad size being shown in the first screen is 468x60 and the banner ad being shown in the second screen is 320x50. This indicates that 2 banner ads are being shown separately. The banner on the first screen should stay open with your current implementation as the activity is not closed. 

Batuhan Savaş Yılmaz

unread,
Sep 6, 2023, 5:02:16 PM9/6/23
to Google Mobile Ads SDK Developers
But in different applications (letgo for example), when you switch from a different page to the home page screen, the ad on the home page is reloaded, that is, the ad on the home page is discarded during the transition. Based on what you said, it is normal that when you switch from the home page to a different page, the ad on the home page is not thrown, that is, it remains open in the background. Wouldn't this be a policy violation? Who should I contact if my admob account is closed for this reason? Google is very strict about policies.



6 Eylül 2023 Çarşamba tarihinde saat 23:53:21 UTC+3 itibarıyla Mobile Ads SDK Forum Advisor şunları yazdı:

Mobile Ads SDK Forum Advisor

unread,
Sep 6, 2023, 7:17:18 PM9/6/23
to batuhansa...@gmail.com, google-adm...@googlegroups.com

Hi,

Thank you for your response.

For the dispose functionality, you may check our sample code snippet (https://developers.google.com/admob/flutter/banner#load_an_ad) for the sample implementation of it. Then, for the policy violation if you encounter any, you may reach out to the Product Support Team (https://support.google.com/admob/thread/new?hl=en) as they are better equipped to handle policy violation. For more information about the Admob policies and violation, you may check this article (https://support.google.com/admob/answer/6128543?hl=en&sjid=8150529771329682226-AP). In addition to that, you may check this recommended banner implementation (https://support.google.com/admob/answer/6275335?hl=en&sjid=8150529771329682226-AP) for your reference. 

Batuhan Savaş Yılmaz

unread,
Sep 7, 2023, 3:22:02 PM9/7/23
to Google Mobile Ads SDK Developers
Thanks for everything.

7 Eylül 2023 Perşembe tarihinde saat 02:17:18 UTC+3 itibarıyla Mobile Ads SDK Forum Advisor şunları yazdı:

Batuhan Savaş Yılmaz

unread,
Sep 8, 2023, 8:36:43 PM9/8/23
to Google Mobile Ads SDK Developers
I tried all kinds of things but it still doesn't work. If possible, can you write a sample code for me? When you switch to the 2nd page, the advert on the first page will be dispose. Thank you.

7 Eylül 2023 Perşembe tarihinde saat 22:22:02 UTC+3 itibarıyla Batuhan Savaş Yılmaz şunları yazdı:

Mobile Ads SDK Forum Advisor

unread,
Sep 11, 2023, 6:23:45 AM9/11/23
to batuhansa...@gmail.com, google-adm...@googlegroups.com
Hi Batuhan,

Thank you for responding back. In order for us to provide guidance precisely, could you kindly share to us a sample project? This will enable us to have full visibility of your implementation as well. Kindly share it to us using the steps below.

1. Navigate to

https://docs.google.com/forms/d/e/1FAIpQLSfkAiXMeYP-fw1W3Z-tT9uwmATEKO5X6S-th0gR2ezdKaaqfg/viewform?usp=pp_url&entry.400550049=Mobile+Ads+SDK&entry.460850823=5004Q00002oikv7QAA&entry.80707362=00197147

2. Fill out all fields, and attach your file(s).

3. Please reply back on this thread when you have uploaded your file(s). Please do not share this link.

Message has been deleted

Batuhan Savaş Yılmaz

unread,
Sep 11, 2023, 7:15:01 AM9/11/23
to Google Mobile Ads SDK Developers
yes i uploaded now. thanks for help

11 Eylül 2023 Pazartesi tarihinde saat 13:23:45 UTC+3 itibarıyla Mobile Ads SDK Forum Advisor şunları yazdı:

Mobile Ads SDK Forum Advisor

unread,
Sep 11, 2023, 1:28:39 PM9/11/23
to batuhansa...@gmail.com, google-adm...@googlegroups.com
Hello,

I'm afraid that the file was not successfully uploaded. Kindly try again.

If the file(s) you are looking to share are less than 25mb in total you can attach them to this case on your next reply. If you are having trouble attaching your file to this case or if your file(s) are larger than 25mb, you can share your files with me by performing the following steps:

Batuhan Savaş Yılmaz

unread,
Sep 11, 2023, 3:12:36 PM9/11/23
to Google Mobile Ads SDK Developers
If you don't mind, could you take a look at my codes in the first message? The whole problem is in those codes.

11 Eylül 2023 Pazartesi tarihinde saat 20:28:39 UTC+3 itibarıyla Mobile Ads SDK Forum Advisor şunları yazdı:

Mobile Ads SDK Forum Advisor

unread,
Sep 11, 2023, 6:23:01 PM9/11/23
to batuhansa...@gmail.com, google-adm...@googlegroups.com
Hello,

We will have to share this to the wider team to provide assistance on this. Rest assured that one of our team will reach out to you.

Mobile Ads SDK Forum Advisor

unread,
Sep 12, 2023, 7:37:55 AM9/12/23
to batuhansa...@gmail.com, google-adm...@googlegroups.com
Hello,

Thank you for providing that code. Take a look at our Banner example project on GitHub. There are some best practices that can be adopted in your code such as using instead of ! and calling dispose() on the BannerAd instance before calling super.dispose() - but to answer your original question the ad is still displaying on the first screen because it has not been removed from the widget tree yet. The second screen is pushed onto the navigation stack with the first screen being displayed again when the second screen is popped off the stack, and removed from the widget tree. This is expected and it is my understanding that you will receive a policy violation for this. 

Thanks,
Justin

ref:_00D1U1174p._5004Q2oikv7:ref

Batuhan Savaş Yılmaz

unread,
Sep 12, 2023, 11:42:38 AM9/12/23
to Google Mobile Ads SDK Developers
Yes, I will try again and if I fail, I will give up. Thanks for everything. Assalam aleykum from Turkey.

12 Eylül 2023 Salı tarihinde saat 14:37:55 UTC+3 itibarıyla Mobile Ads SDK Forum Advisor şunları yazdı:
Reply all
Reply to author
Forward
0 new messages