public static void waitForActivityFinishing(Activity activity)
      throws InterruptedException, TimeoutException {
    long deadline = SystemClock.uptimeMillis() + UI_ACTION_EFFECT_TIMEOUT_MILLIS;
    while (!activity.isFinishing()) {
      long millisTillDeadline = deadline - SystemClock.uptimeMillis();
      if (millisTillDeadline < 0) {
        throw new TimeoutException("Timed out while waiting for activity to start finishing");
      }
      Thread.sleep(50);
    }
  }
Waits until the {@link Activity} is finishing.
javatests/com/google/android/apps/authenticator/testing/TestUtilities.java