Espresso – Properly clear Activities saved instance states

Issue

I am writing espresso tests for my app and having difficulties clearing the app state, state gets saved and this affects other tests and brings flakiness to them.

I use intent rule for starting my activity:

    @Rule
    public final IntentsTestRule<MainActivity> mIntentsTestRule  newIntentsTestRule<>(MainActivity.class, true, false);

My setUp clears everything that I have access to: db, sharedPrefs, custom volatile config.

    @Before
    public void setUp() {
     mSharedPrefManager.clear();
     mAppConfig.clear();
     mDatabasePersister.deleteDatabase();
    }

Sadly I have issues with activity saved instance state, this brings flakiness to my tests since previous states are retained.

Is there a way to clear the saved instance states programmatically for each activity start?

Solution

Issue was caused by a memory leak, so the activity was kept without properly clearing it, thanks for the effort, after resolving the leak espresso ran fine.

Answered By – originx

Leave a Comment