SHARE
TWEET

Untitled

a guest Aug 13th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class mYApplication extends MultiDexApplication {
  2.  
  3.  @Override
  4.     public void onCreate() {
  5.         super.onCreate();
  6.         registerActivityLifecycleCallbacks(new MyLifecycleHandler());
  7.  
  8.      }
  9.  
  10. }
  11.    
  12. public class MyLifecycleHandler implements Application.ActivityLifecycleCallbacks {
  13.     private static int resumed;
  14.     private static int paused;
  15.     private static int started;
  16.     private static int stopped;
  17.  
  18.     @Override
  19.     public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
  20.     }
  21.  
  22.     @Override
  23.     public void onActivityDestroyed(Activity activity) {
  24.     }
  25.  
  26.     @Override
  27.     public void onActivityResumed(Activity activity) {
  28.         ++resumed;
  29.     }
  30.  
  31.     @Override
  32.     public void onActivityPaused(Activity activity) {
  33.         ++paused;
  34.         android.util.Log.w("test", "application is in foreground: " + (resumed > paused));
  35.     }
  36.  
  37.     @Override
  38.     public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
  39.     }
  40.  
  41.     @Override
  42.     public void onActivityStarted(Activity activity) {
  43.         ++started;
  44.     }
  45.  
  46.     @Override
  47.     public void onActivityStopped(Activity activity) {
  48.         ++stopped;
  49.         android.util.Log.w("test", "application is visible: " + (started > stopped));
  50.     }
  51.  
  52.     public static boolean isApplicationVisible() {
  53.         return started > stopped;
  54.     }
  55.  
  56.     public static boolean isApplicationInForeground() {
  57.         return resumed > paused;
  58.     }
  59.  
  60.  
  61. }
RAW Paste Data
Top