I'm trying to write an Android activity instrumentation test that stops (onPause()
, then onStop()
) and restarts the current activity. I tried
activity.finish();
activity = getActivity();
...but that doesn't seem to work properly.
The goal of the test is to assert that form data is stored during the onPause()
method and re-read during the onStart()
method. It works when doing it manually, but the test fails, from which I draw the conclusion that activity.finish()
seems to be the wrong way to stop and restart an activity.
Edit: My main problem seems to have been a synchronization issue. After restarting the activity, the test runner didn't wait for all event handlers to finish. The following line halts the test execution until the activity is idle:
getInstrumentation().waitForIdleSync()
Besides that, take a look at the accepted answer for more valuable information about the lifecycle.