Ensuring a Fresh State for Each UI Test in Robotium

Ensuring a Fresh State for Each UI Test in Robotium

11 April 2024 Stephan Petzl Leave a comment Tech-Help

When conducting UI testing using Robotium, it’s crucial to ensure that each test starts with a fresh state. This is especially important when running batches of tests, such as during Continuous Integration (CI) builds, where the state of the application after one test can undesirably affect the subsequent ones. The following guide provides a solution to this common challenge.

Problem Overview

In a suite of UI tests using Robotium, it’s often observed that the state of the application is carried over from one test to the next. This is because the application isn’t killed after each test, which may lead to failures in subsequent tests that assume a fresh state. The goal is to ensure that each test in the suite starts with the application in its initial state.

Solution: Ensuring a Clean Slate

To address this issue, we need to implement a teardown process that effectively resets the application state after each test. The recommended approach is to utilize the solo.finishOpenedActivities() method within the teardown phase of your testing code. Below is a practical example of how to apply this method:

Implementing the Teardown Method

public void tearDown() throws Exception {



By calling solo.finishOpenedActivities(), you’re instructing Robotium to close all of the activities that were opened during the test. This method ensures that the application is brought back to its base state, ready for the next test to run without any residual effects from the previous one.

Additional Considerations

While the above solution is often sufficient, there may be cases where activities are started before Robotium has a chance to monitor them. In such instances, it can be helpful to manually simulate a user pressing the back button to close the app, as shown in the following snippet:

private void backOutToHome() {
    boolean more = true;
    while(more) {
        try {
        } catch (SecurityException e) { // Done, at Home.
            more = false;

This method sends a series of back button presses to the application until it is effectively closed, emulating what a user would do to exit the app.


By incorporating the solo.finishOpenedActivities() method into your Robotium teardown process, you can greatly improve the reliability of your UI tests by ensuring that each one starts with a clean state. Remember to adapt the teardown process to the specific needs and behavior of your application to achieve the best results.

Like this article? there’s more where that came from!