After reading all the answers for questions like "how can I know if my Android app is in the background/foreground" I am still unsure what to do while an interstitial is running?
The preferred answer is basically to track it yourself by manually tracking onPause
and onResume
or using the new lifecycle
library from Google. This works well, however this assumes you control the code for all the activities, when an interstitial runs that isn't your own Activity
so this method won't work for it.
Other answers involve calling ActivityManager.getRunningAppProcesses()
or ActivityManager.getRunningTasks()
. These are of course not recommend and shouldn't be used. I'm also guessing future policy changes might limit this.
So how can I know if my app is on the foreground even while showing an interstitial ad?
Since someone might ask why I need to know this. My app uses the WebView
. The WebView
has a nasty habit of letting Javascript
continue to run even when onPause()
is called on the WebView
. The only way I have found of stopping it is to run webView.pauseTimers()
but that method applies to all WebViews, not just the one you called it on. So if I call that on when my onPause()
is called on my Activity
right before an interstitial ad, then the interstitial ad will probably freeze and might even ANR.