What the best way to check if an Android application is in foreground?
<p>I have an application that uses AsyncTasks to make REST calls to a server. Whenever the AsyncTask is finished, it either: shows an error dialog/calls the next activity (if the application is in foreground) or does nothing (application is in background).</p>

<p>I do this to avoid having an activity poping out after the application is sent to background.</p>

<p>I've seen some implementations:</p>

<ol>
<li><a href="http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not">check android application is in foreground or not?</a></li>
<li><a href="http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service">Determining the current foreground application from a background task or service</a></li>
</ol>

<p>Currently i'm using another, even simpler: </p>

<ul>
<li>onResume of each activity I set a global variable as FOREGROUND</li>
<li>onPause of each activity I set a global variable as BACKGROUND</li>
</ul>

<p>Which would you recommend?</p>

<p>PS
I am aware of the best practice of REST calls as described in this video <a href="https://www.youtube.com/watch?v=xHXn3Kg2IQE&amp;list=PL73AA672847DC7B35" rel="nofollow">https://www.youtube.com/watch?v=xHXn3Kg2IQE&amp;list=PL73AA672847DC7B35</a> and in my specific case this approach seems more appropriate.</p>
