title: Get Current Activity in Espresso android
body: <p>In case of a test that crosses multiple activities, is there a way to get current activity? </p>
  
  <p>getActivtiy() method just gives one activity that was used to start the test.</p>
  
  <p>I tried something like below,</p>
  
  <pre><code>public Activity getCurrentActivity() {
      Activity activity = null;
      ActivityManager am = (ActivityManager) this.getActivity().getSystemService(Context.ACTIVITY_SERVICE);
      List&lt;ActivityManager.RunningTaskInfo&gt; taskInfo = am.getRunningTasks(1);
      try {
          Class&lt;?&gt; myClass = taskInfo.get(0).topActivity.getClass();
          activity = (Activity) myClass.newInstance();
      }
      catch (Exception e) {
  
      }
      return activity;
  }
  </code></pre>
  
  <p>but I get null object.</p>