title: How to hook into Android Application lifecycle?
body: <p>Android supports hooking into the <code>Activity</code> lifecycle through <a href="https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html" rel="nofollow noreferrer"><code>Application.ActivityLifecycleCallbacks</code></a> but I couldn't find a way to <em>easily</em> hook into the <code>Application</code> one. Why isn't there a similar interface for the <code>Application</code> lifecycle?</p>
  
  <p>I want to know if an app is in background/foreground and I don't want to do the count manually (feasible using the <a href="https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html" rel="nofollow noreferrer"><code>Application.ActivityLifecycleCallbacks</code></a>).</p>
  
  <p>I'm developing an Android library and one of the use cases requires knowing if the app which includes the lib is in background/foreground.</p>
  
  <p>I saw that there is an <a href="https://developer.android.com/reference/android/support/test/runner/lifecycle/ApplicationLifecycleCallback.html" rel="nofollow noreferrer"><code>ApplicationLifecycleCallback</code></a> interface available in the <code>android.support.test.runner.lifecycle</code> package (only for testing). Why not exposing something similar within the <code>android.app</code> package as <code>Application.ActivityLifecycleCallbacks</code>?</p>
  
  <p>How to accomplish my use case? How would you implement it?</p>
  
  <p>Any hints/ideas/workarounds would be more than welcome. Thanks a lot!</p>