title: Android detecting if an application entered the background
body: <p>I'm trying to implement some automatic logout code for my Application on Android.</p>
  
  <p>I need to detect if all the activities belonging to an Application have entered the background as opposed to working with <code>onPause()</code> and <code>onResume()</code> for each individual activity. iOS has a helpful <code>applicationDidEnterBackground:</code> method that I could utilize, but I'm unable to find a similar function in Android's <code>Application</code> class.</p>
  
  <p>One approach seems to be to have an <code>AtomicInteger</code> counter and increment it once an activity becomes visible and decrement it when it's finished or onStop() gets called. So if the counter becomes zero, I can start a service that runs in the background and handles the logout. Is this how it's usually done?</p>