title: Testing if a recently launched activity actually has started
body: <p>I have looked into a few similar questions on SO, but have not yeet managed to successfully test this.</p>
  
  <p><a href="https://stackoverflow.com/questions/5945677/android-testing-if-another-activity-has-started">Android - testing if another activity has started</a></p>
  
  <p>I tried this one, but for me, origactivity turns out to be null, so I used .baseIntent.getComponent() instead to identify the started activity(I suppose this should give me the same result?)</p>
  
  <p>My JUnit test code:</p>
  
  <pre><code>public class MainMenuTest extends
          ActivityInstrumentationTestCase2&lt;MainMenuActivity&gt; {
  
      public MainMenuTest() {
          super("package.name", MainMenuActivity.class);
      }
  
      private MainMenuActivity mActivity;
      private ActivityManager am;
  
      public void setUp() throws Exception {
          super.setUp();
          mActivity = this.getActivity();
          am = (ActivityManager) mActivity.getSystemService(Service.ACTIVITY_SERVICE);
          Intent i = new Intent(mActivity, GameActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          mActivity.startActivity(i);
      }
  
      public void testNewGameButton() {
          final ActivityManager am2 = am;
  
          mActivity.runOnUiThread(new Runnable() {
              public void run() {                 
                  List&lt;ActivityManager.RecentTaskInfo&gt; processes = am2.getRecentTasks(2, ActivityManager.RECENT_WITH_EXCLUDED);
                  ActivityManager.RecentTaskInfo recentTask = processes.get(0);
                  assertEquals(recentTask.baseIntent.getComponent().getClassName(), GameActivity.class.getName());
              }
          });
      }
  }
  </code></pre>
  
  <p>Unfortunately, this causes the <code>assertEquals(recentTask.baseIntent.getComponent().getClassName(),</code> assertion to fail.</p>
  
  <p>Why is this, and is there a better way to test this?</p>