title: Espresso not waiting till Activity is destroyed, before creating a new one for the next test
body: <h2>The Situation</h2>
  
  <p>In the official documentation here: <a href="https://google.github.io/android-testing-support-library/docs/rules/index.html" rel="noreferrer">https://google.github.io/android-testing-support-library/docs/rules/index.html</a>, it says:</p>
  
  <blockquote>
    <p>"This rule provides functional testing of a single activity. The
    activity under test will be launched before each test annotated with
    @Test and before any method annotated with @Before. <strong>It will be
    terminated after the test is completed and all methods annotated with
    @After are finished</strong>. The Activity under Test can be accessed during
    your test by calling ActivityTestRule#getActivity()."</p>
  </blockquote>
  
  <p>Technically yes, the Activity is being terminated. <strong>But there doesn't seem to be any guarantee as to when this will happen</strong>. E.g. it won't necessarily happen before it's created again for the next test.</p>
  
  <hr>
  
  <h2>The Problem</h2>
  
  <p>In some of my tests, I need to rely on the fragments OnDestroy or OnDetach being called after each test, <strong>before</strong> the next test starts. I have listeners that need to be cleared and recreated.</p>
  
  <p>If onDestroy from the previous test is called after OnResume in the current test, then the callback is cleared and the view doesn't get updated and the test fails.</p>
  
  <p>If onDestroy from the previous test is not called at all, then the callback from the current test will be referring to the wrong instance. Again the view will not get updated and the test will fail.</p>
  
  <hr>
  
  <h2>The Question</h2>
  
  <ol>
  <li><strong>Is this behaviour discussed in the situation by design or is it a bug?</strong> I'm so far unable to find this in the documentation.</li>
  <li>What is best practice to handle this? I suspect other people have faced this problem.</li>
  </ol>
  
  <p><strong>Edit</strong>: I've now solved part 2. See workarounds section below. However if someone can answer part one by citing an official resource then I'd be happy to accept that answer. That's what I'm really asking here. The second part was just a bonus if someone had some ideas.</p>
  
  <hr>
  
  <h2>The Proof</h2>
  
  <p>If you would like to see this behaviour it will only take a few moments. Create a new project with an Activity like this:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
      }
  }
  </code></pre>
  
  <p>and a test class like this:</p>
  
  <pre><code>@RunWith(AndroidJUnit4.class)
  @LargeTest
  public class EspressoLifecycleTest {
  
      @Rule
      public ActivityTestRule&lt;MainActivity&gt; mActivityRule =
          new ActivityTestRule&lt;&gt;(MainActivity.class);
  
      @Test
      public void test1() {
      }
  
      @Test
      public void test2() {
      }
  
      @Test
      public void test3() {
      }
  
      @Test
      public void test4() {
      }
  }
  </code></pre>
  
  <p>Put breakpoints on the OnResume and OnDestroy methods and run the test suite in debug mode.</p>
  
  <p>Do this a few times and notice that the order the Activity life cycle methods are called is not consistent. E.g. it might call OnResume twice in a row, and then call OnDestroy once, and then OnResume twice again, and then OnDestroy three times, or any other combination you can think of. Of course it always starts with at least one OnResume. Sometimes it doesn't even call OnDestroy if it's at the very end, but that's fine. What's not fine is that my tests are flaky because of this unpredicatable order.</p>
  
  <p>I'm aware that this might be intentional and there could be a simple way to deal with it, I'm just not lucky enough to have found it. Please if you know what it is, post the answer here. I don't care how dumb my question might be in hindsight, I've spent a LOT of time on this problem. It's almost always something simple so I'm prepared to be embarrassed by the answer.</p>
  
  <hr>
  
  <h2>Workarounds</h2>
  
  <p>Using onPause over OnDestroy has the side effect of being called when I startActivityForResult, but without calling onResume again in the background fragment while in tablet mode. I'm exploring ways to make this work but no solution as yet.</p>
  
  <p><strong>Edit:</strong> onPause ended up with the same problem - which is partly why I was using onDetach in the first place. Ultimately, there are times when I don't want to detach the listeners until the fragment is destroyed.</p>
  
  <p><strong>This leads me to my next idea which worked! Hooray!</strong> Up until now I was creating a callback for the calling Activity, for the thing it was asking for, <strong><em>only</em></strong> if that specific callback didn't exist. It turns out this was a bad idea. I did that so I could limit the number of callbacks to the exact number required. The motivation was sound but the implementation required all this callback clearing. The solution is to recreate every callback when ever it's called from the fragment. Don't create it if it's null, <strong>always</strong> create it and replace whatever was there before. Now there's no need to clear them at all (AFAIK).</p>