Search code examples
androidrobotiumjunit3

Robotium Activity Leak Causing Wrong Activity for other Tests


I am having some issues with my Robotium tests. I have 3 test classes that each have about 2 to 3 methods. They all test different activities in the Android Life Cycle. The problem I am finding is that when test A presses a button in A the intent is called for the next activity the test passes and then finishes. Then test B tries to start an activity. I noticed then that the Activity from test A is still alive. I tried using this to help the problem. The idea would be that the device would wait till the activity was idle then I will end any inactive activities. Then I would end any ones that were open and left.

 getInstrumentation().waitForIdleSync();
 solo.finishInactiveActivities();
 solo.finishOpenedActivities();

I am not sure if this is 100% the best way of taking care of this problem. I have noticed that it works 80% of the time but I am still getting some leaks.


Solution

  • I solved the problem by using the solo.goBack(); function call. This will kill all of the open activities so that there is no leak of activities during your testing. As well I used the code I had placed above as double cover. After repeating these tests on numerous devices and scenarios it seems to pass.