title: Android checking whether the app is closed
body: <p>I have an android application i need one function or any broadcast receiver that can check if the app is closed.. i don't need to call on destroy in every activity (there is about 20 activity into the app)
  i tried to add this function in <strong>Application class</strong></p>
  
  <pre><code>public class ApplicationLifeCycleManager implements ActivityLifecycleCallbacks {
  
  /** Manages the state of opened vs closed activities, should be 0 or 1.
   * It will be 2 if this value is checked between activity B onStart() and
   * activity A onStop().
   * It could be greater if the top activities are not fullscreen or have
   * transparent backgrounds.
   */
  private static int visibleActivityCount = 0;
  
  /** Manages the state of opened vs closed activities, should be 0 or 1
   * because only one can be in foreground at a time. It will be 2 if this
   * value is checked between activity B onResume() and activity A onPause().
   */
  private static int foregroundActivityCount = 0;
  
  /** Returns true if app has foreground */
  public static boolean isAppInForeground(){
      return foregroundActivityCount &gt; 0;
  }
  
  /** Returns true if any activity of app is visible (or device is sleep when
   * an activity was visible) */
  public static boolean isAppVisible(){
      return visibleActivityCount &gt; 0;
  }
  
  public void onActivityCreated(Activity activity, Bundle bundle) {
  }
  
  public void onActivityDestroyed(Activity activity) {
      Log.wtf("destroyed","app closed!!");
  }
  
  public void onActivityResumed(Activity activity) {
      foregroundActivityCount ++;
  }
  
  public void onActivityPaused(Activity activity) {
      foregroundActivityCount --;
  }
  
  public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
  }
  
  public void onActivityStarted(Activity activity) {
      visibleActivityCount ++;
  }
  
  public void onActivityStopped(Activity activity) {
      visibleActivityCount --;
  }
  }
  </code></pre>
  
  <p>Also i have registered in on create in <strong>Application class</strong></p>
  
  <pre><code>@Override
  public void onCreate() {
      super.onCreate();
      registerActivityLifecycleCallbacks(new ApplicationLifeCycleManager());
  }
  </code></pre>
  
  <p>but the onPaused and onResumed and onDestroyed function is called  when i switch between activity: because it detects whether any activity is closed or destroyed or even resumed</p>
  
  <p>so any solution to check whether the app is closed in one function??</p>