Android auto-logout when app goes to background
<p>I want to detect when my application is sent to the background. There are many questions about hooking the HOME key - I understand this is possible only by registering as a launcher app.</p>

<p>...BUT... as always there is a client who wants certain behaviour...</p>

<p>We have an app with high security requirements. The client wants the app to log out of the server whenever the app goes into the background for whatever reason (phone call, HOME key, back on last activity) (<strong>* *<em>to clarify I mean that when the front Activity on the screen is not one of my app's activities</strong> *</em>*).</p>

<p>So, if I can't hook the HOME key, what other options are there? Obviously just hooking <code>onPause()</code> won't help, because that is <code>Activity</code>-specific. </p>

<p>The "best" we have come up with is to keep an array of Activity references in our Application class. In each Activity's <code>onResume()</code> we add it to this array. In <code>onPause()</code> we remove it. Also in <code>onPause()</code> we enumerate through this array to find out if any of the registered activities are in the foreground. If no foreground activity is found, user gets logged out.</p>

<p>I am unhappy with this as a solution, and hope to find a better way.</p>
