For a half workaround, see post here: http://community.monogame.net/t/workaround-monogame-app-crashes-when-task-switched-with-admob-ad-visible/9981/5?u=trinith
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.
- Create a new MonoGame Android Application (I did this in Visual Studio 2017 with MonoGame 3.6 installed)
- Add a NuGet package for Xamarin.GooglePlayServices.Ads
- Replace the code in Game1.cs with the code found here: https://pastebin.com/cYkMCfjT
- 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
- Run the code on a simulator. I'm using Nexus_5X_API_24 (Android 7.0 - API 24) that I created via Android Studio.
- Once the program starts, wait until the rectangle turns green.
- Task switch to the home screen.
- Task switch back to the test game. Observe that it comes back as expected.
- Click the green rectangle. A test ad will appear.
- Click the X to close the test ad. Observe that the application re-appears.
- Perform a task switch away and back. Observe that the application re-appears.
- Click the green rectangle to show another test ad.
- Without closing the ad, task switch to the home screen.
- Task switch back to the app. The ad is currently visible.
- 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.