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.
By calling (or trigger a screen orientation change):
Causing the activity go through the complete recreation life cycle:
What you need is:
I exposed the Instrumentation API recently and found plenty of interesting activity life cycle trigger method callActivityOnXXX(), the following single line of code should do the tricky:
Activity life cycle diagram quoting from official dev guide:
