Android AdMob Bug

Posted by in Software

I’m trying to implement AdMob in my application as an exercise. I followed the tutorial to the letter (read: copy/pasted code from the sample into my app). I ended up with an activity, where onCreate() had the following code:

and a layout like this:

Pretty simple. However, I found that after rotating the screen several times, I would end up with a bunch of Activity instances hung by some ad threads. Not nice.

My salvation came from this StackOverflow answer. Apparently, null-ifying the references by hand and removing the ad view from the layout allows the activity to finish. The destroyAdView() below has the gist:

Note: One thing to note is if you test on a physical device and rotate the screen, even after a GC trigger you may still have several references up and running. You’re OK as long as you have Finalizer classes. This means that whatever the ad process is doing hasn’t finished yet. If you wait a bit more, the finalizer queue empties and the ad instances go away.

Some links to feed your apetite:


A little experiment: If you find this post and ad below useful, please check the ad out 🙂