Created
August 15, 2016 20:40
-
-
Save tir38/1a1c479b1f61e8b5b76c63eaadbf5dd4 to your computer and use it in GitHub Desktop.
A proper cleanup of http://qathread.blogspot.com/2014/09/discovering-espresso-for-android-how-to.html for Espresso 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.support.annotation.Nullable; | |
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; | |
import java.util.Collection; | |
import static android.support.test.InstrumentationRegistry.getInstrumentation; | |
import static android.support.test.runner.lifecycle.Stage.RESUMED; | |
public final class CurrentActivityUtil { | |
@Nullable | |
public static Activity getCurrentActivity() { | |
final Activity[] currentActivity = {null}; | |
getInstrumentation().runOnMainSync(() -> { | |
Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED); | |
if (resumedActivities.iterator().hasNext()) { | |
currentActivity[0] = resumedActivities.iterator().next(); | |
} | |
}); | |
return currentActivity[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment