title: Using Espresso idling resource with multiple activities
body: <p>I have a firstActivity that launches the secondActivity, where in the secondActivity I have a loading Dialog (not AsyncTask), and I need to make Espresso wait until the dialog disappears before it continues with the test.<br>
  Where do I have to implement the IdlingResource? How can I make it wait for the dismissDialog() function? </p>
  
  <p>Here is what I've tried to do:</p>
  
  <pre><code>  class DocumentLoadingIdlingResource implements IdlingResource {
      private ResourceCallback callback;
  
      @Override
      public String getName() {
        return "Documnet loading idling resource";
      }
  
      @Override
      public boolean isIdleNow() {
        Activity activity;
        try {
          activity = getCurrentActivity();
        } catch (Throwable e) {
          return false;
        }
  
        if(activity.getClass().getName().equals(EditorActivity.class.getName())
              &amp;&amp; activity.loadingDialogShowing() == false) {
          return false;
        }
  
        return true;
      }
  
      @Override
      public void registerIdleTransitionCallback(ResourceCallback callback) {
        this.callback = callback;
      }
    }
  
    Activity getCurrentActivity() throws Throwable {
      getInstrumentation().waitForIdleSync();
      final Activity[] activity = new Activity[1];
      runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
          java.util.Collection&lt;Activity&gt; activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
          activity[0] = com.google.common.collect.Iterables.getOnlyElement(activites);
      }});
      return activity[0];
    }
  </code></pre>
  
  <p>This class is implemented in the test class.</p>