Kamil Krzyk, thanks for sharing!

I don’t believe the following is a good way of getting the current activity though

public Activity getCurrentActivity() {        return ((TestApplication) InstrumentationRegistry.            getTargetContext().getApplicationContext()).getCurrentActivity();    }

Inside the Espresso test, one can do the following which is better:

protected Activity getActivityInstance() {
final Activity[] currentActivity = new Activity[1];
getInstrumentation().runOnMainSync(() -> {
Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance()
for (Activity act : resumedActivities) {
currentActivity[0] = act;

return currentActivity[0];

Do you have a Github repository for me to understand your tutorial better?

Hiring Android engineer shorturl.at/bivJO Clean coder, walker & biker. Hater of inefficiency

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store