Flutter interstitial ad with AppOpen ads

Viewed 209

Currently Im using native_admob_flutter for my flutter app. I also included AppOpenAds and Interstital ad within the app.

I want to display the AppOpen ad when user open the app/ resume to the app, here is how I do it in main.dart:

@override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {     
switch (state) {
  case AppLifecycleState.resumed:
    loadAndDisplayAppOpenAd();
    break;
  case AppLifecycleState.inactive:
    break;
  case AppLifecycleState.paused:
    break;
  case AppLifecycleState.detached:
    break;
}
}

However I found that displaying Interstial ad will also change the app state -> inactive -> paused -> resume, which trigger AppOpen ad.

This really create bad user experinence. Is there a way to overcome this? Thank you!

1 Answers

You may need a variable to make sure that app has been paused by user action, and not by interstitial.

static bool pausedByInterstitial = false;
...
class AppLifecycleReactor extends WidgetsBindingObserver {
  final AppOpenAdManager appOpenAdManager;

  AppLifecycleReactor({required this.appOpenAdManager});

  static bool pausedByInterstitial = false;

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {

    if (state == AppLifecycleState.resumed && !pausedByInterstitial) {
      appOpenAdManager.showAdIfAvailable();
    }
  }
}
...
showInter() {

    AppLifecycleReactor.pausedByInterstitial = true;

    
    InterstitialListener(
         ...
         onAdHiddenCallback: (ad) {
              AppLifecycleReactor.pausedByInterstitial = false;
         },
         ...
    );
}
Related