[WORKAROUND-ISH] MonoGame app crashes when task switched with AdMob ad visible

For a half workaround, see post here: [WORKAROUND-ISH] MonoGame app crashes when task switched with AdMob ad visible

I’m trying to implement interstitial ads in my game using AdMob and I’m having some trouble. It turns out that if an ad is currently showing and you task switch (either via task switch buttons or by simply clicking the ad), the MonoGame application dies.

I’ve created some code for a simple test application that demonstrates the issue.

  1. Create a new MonoGame Android Application (I did this in Visual Studio 2017 with MonoGame 3.6 installed)
  2. Add a NuGet package for Xamarin.GooglePlayServices.Ads
  3. Replace the code in Game1.cs with the code found here: https://pastebin.com/cYkMCfjT
  4. Replace the code in Activity1.cs with the code found here: https://pastebin.com/Raad2rYM
    NOTE: You will need an Ad Unit ID that you get from AdMob, or some other public test ID. I have one but I’m not sure if I’m supposed to share… new to this :slight_smile:
  5. Run the code on a simulator. I’m using Nexus_5X_API_24 (Android 7.0 - API 24) that I created via Android Studio.
  6. Once the program starts, wait until the rectangle turns green.
  7. Task switch to the home screen.
  8. Task switch back to the test game. Observe that it comes back as expected.
  9. Click the green rectangle. A test ad will appear.
  10. Click the X to close the test ad. Observe that the application re-appears.
  11. Perform a task switch away and back. Observe that the application re-appears.
  12. Click the green rectangle to show another test ad.
  13. Without closing the ad, task switch to the home screen.
  14. Task switch back to the app. The ad is currently visible.
  15. Click the X button to close the ad. The application switches to the home screen and the app is seemingly unrecoverable.

Here is a paste of the entire log I get when I run this code using the steps above. I’ll admit I don’t really understand what’s going on, but from the googling I’ve been doing and the breakpoints I’ve been setting, it looks like the activity itself is crashing, leaving only the ad behind.

Log dump: << See reply >>*

The reason I suspect that this has to do with MonoGame is that I’ve recreated this application using a Xamarin Android App using a simple activity containing a button, as well as an OpenGL activity, and have not been able to reproduce it. I only get this behaviour when using MonoGame.

I’m hoping there’s a setting I’m missing here because thus far I’ve been unable to work around this. The crash happens when I task switch when the ad is showing, so I’ve already gotten the OnPause event from the acitivity and I’m not sure how to get an OnPause event from the ad itself, in order to detect when that is task switched.

I’m thinking this might actually be a bug within MonoGame, but this is new territory for me so it could just as easily be user error. Has anybody else encountered this or have any ideas? If this is entering possible bug territory, if there’s any way I can help the devs track down the issue, I’m happy to help.


  • Apparently new users can only put two links in a post, so I’ll add a link to the log dump in a reply, if it lets me.

Log dump: https://pastebin.com/qRqAQFD6

Ok, I’ve done a lot more research… by which, of course, I mean furiously hacking away. I’m not entirely sure if the application is crashing… something is going wrong; however, both the activity and the game objects are still alive and can provide data. For some reason, the view just doesn’t show anymore… and I have no idea why.

I discovered that with this bit of code, you can actually detect this state.

    private bool CheckIfApplicationAlive()
        bool appAlive = false;

            View gameView = _game.Services.GetService<View>();
            if (gameView == null || gameView.IsShown == false)
                throw new Exception("Game view did not show after ad close.");

            appAlive = true;
        catch { }

        return appAlive;

I put a call to this method in my AdListener’s AdClosed event (routed from AdListener.OnAdClosed). Through testing, I’ve discovered that under normal circumstances, the game’s view comes back after an ad is closed and IsShown will be true. Under this weird state, IsShown will be false after the ad closes.

So when this happens, I can do something… what that something is has proven to be quite a challenge! I tried calling the Activity’s Finish and FinishAndRemoveTask methods; however, something is keeping the task open somehow. It shows up in the task list and keeps my Visual Studio debugging session open. About the absolute best I’ve managed to do is to create a new activity that shows an error message, then show that activity. This error message summed up is pretty much “I don’t even… sorry… please kill application manually and don’t hate me forever.”

Anyway, something is definitely up here… hopefully others have some ideas.


Another update… but first, a question.

Is this cool? I’m trying to provide as much information on this as possible in the hopes that, if there is an issue within MonoGame here, the devs have as much information as possible for their investigations. Of course, I also get a notification when I’m posting that I shouldn’t post several posts in a row… I’m new here so if this isn’t cool, please let me know what the accepted approach is :slight_smile:

For now though, the results of my latest experiment. Since I’ve confirmed in a previous test that the issue doesn’t present itself when performed from a normal Android application (Xamarin), I figured I would try having my game activity launch a different activity that would be responsible for showing ads. I put all the ad stuff inside that activity so that nothing was in my main game, then when the button was clicked, I launched the separate activity. I still got the issue… as soon as I closed the ad after task switching back, the application quit out to the main screen again.

I’m not sure if this is helpful… but maybe it will help somehow? I dunno… but that’s all I’ve got for now.

I still have no idea what’s causing this, but I have discovered a half workaround. By half, I mean it will allow recovery if you task switch while an ad is open, but only if you didn’t click the ad itself (which launches a web browser linked to your main activity).

The general idea is as follows…

  • Create a new activity, MainActivity, to serve as a host for the application. This has MainLauncher = true set in the attributes and does not launch the mono game class. Ensure LaunchMode is set to SingleInstance as well.
  • Remove MainLauncher = true from your MonoGame Activity… say, MonoGameActivity.
  • In MainActivity.OnResume, have it switch to the game activity the first time, but all other times have it just show an Ad.
  • In the OnAdClosed method, have it switch to the game activity.
  • In the game activity, when it’s time to show an ad, have it change to MainActivity. This will trigger an ad to show and, when that ad closes, it will switch back to the game activity.

Here is the code that makes up my MainActivity class: https://pastebin.com/5GEh73GR
In the Activity1.cs code above, I got rid of all the references to the InterstitialAd object and, in ShowAd, I simply put the following code:

    Intent i = new Intent(this, typeof(MainActivity));

This sort of works, but isn't ideal because you can still get the issue if you click the ad, then task switch back to the game without closing the web browser that was launched. If you close the web browser first, everything comes back fine, but if you leave the browser open, it breaks.

So it's something... at least?

Bad news, everyone :frowning:

Looks like the above workaround doesn’t help under the following scenario…

  • Show ad
  • Click ad (triggers a web browser to open).
  • Without closing the web browser, task switch back to the game.

If you close the web browser first, everything is restored, but if you don’t, you get the same issue as above.


It’s something, but it’s definitely not ideal to have this issue occur when a user opens an ad. It seems a very reasonable use case that someone would click the ad, then task switch back to the game. Users shouldn’t be punished in this way.

This issue is quite frustrating. Not sure what to try next… maybe something will come to me later. I’ll report back with findings if anything comes of it.

Extra bad news… the work around isn’t really sound. It leads to other problems with resuming the game via tapping the icon instead of picking it from the task list. I can see ways to solve this; however, it’s really just another band-aid to a workaround that doesn’t entirely fix the issue anyway.

I really need some help here, and am disappointed in the lack of response and engagement by the dev team (or anyone) on this issue. I’m quite at a loss on how to proceed.

*Edit: I can’t seem to edit the original title anymore. Can someone please remove the “[WORKAROUND-ISH]” component, as the proposed workaround is not correct.


I just read your posted issue in github. First of all I just want to say that the emulator never should be trusted. Always test with a real device. (I don’t know if you’ve already done that already).

Once said that, I’ve tested your code in a tablet and the problem differs slightly to what you mentioned, but still is a problem.

However moving to LaunchMode.SingleTop instead of LaunchMode.SingleInstance seems to fix the problem, at least in my device. (actually it’s the mode I use in the games I’ve released, and never had a similar report)

I hope it helps.

p.s. I had to use AdMob 42.1021.1 because I couldn’t install the latest (I don’t have the latest SDK)

1 Like

I unfortunately don’t have a physical Android device and have thus far been doing all of my development on the emulator. I’m working on getting a device, but haven’t been able to yet. I appreciate the advice though and hopefully can get one soon :slight_smile:

Having said that, I gave your advice a try and switched my LaunchMode to SingleTop instead of SingleInstance and this seems to have resolved the issue, even on my emulator. This is kinda huge, thank you so much!

I’ll be doing some research to better understand this property, but I don’t suppose you can tell me what this means, and why this makes a difference?

Seriously though, thank you…!

I’m glad it worked =)

Honestly, I can’t remember. A year ago or so I had a similar problem with android achievements notifications. I had a look at it and choose SingleTop (basically it was the only one which allowed me to use achievements and adverts without problems). The knowledge has been out of the cache for some time now.

I keep the link that cast some light over this issue for me, though =)

1 Like

Cool, I will take a look at that. Thanks!