ActivityMonitor gets hit but getLastActivity returns null

582 views Asked by At

I have a problem with one of the test cases in my test suite. What I'm trying to do is simply to click on a button and wait for a new activity to launch.

ActivityMonitor monitor = getInstrumentation().addMonitor(MyActivity.class.getName(), null, false);
TouchUtils.clickView(this, searchButton);
Activity a = getInstrumentation().waitForMonitorWithTimeout(monitor, 60);
assertNotNull("The MyActivity is null.", a);

This code should according to the documentation wait up to 60 seconds for the monitor to get hit. If it gets hit the activity will be returned (and otherwise null).

What happens is that every now and then the test passes, but most of the time it fails because a == null. I'm watching the test case as it runs and I can clearly see that it does not wait for 60 seconds. Only a few. With some print outs I also noticed that the monitor in fact gets hit, but the activity returned by getLastActivity() is still null.

1) How is this possible?

2) And is there something I can do to avoid it?

0

There are 0 answers