code: private void startActivityTransitionTimer(final Context applicationContext) {
  
          Timer activityTransitionTimer = new Timer();
  
          activityTransitionTimerTask = new TimerTask() {
  
              public void run() {
                  wasInBackground.set(true);
                  startTime.set(0);
                  isApplicationForegrounded.set(false);
                  listener.onBackgrounded(applicationContext);
              }
          };
  
          activityTransitionTimer.schedule(activityTransitionTimerTask,
                  MAX_ACTIVITY_TRANSITION_TIME_MS);
      }
docs: Start timer to measure time between onPause and onResume to determine if app was backgrounded/closed