2

How can I use Robotium to verify that my Activity handles onPause(), onDestroy(), onResume() and similar calls correctly?

In my Robotium test I can call stuff like

solo.getCurrentActivity().onKeyDown(0, null);

but how can I simulate an activity being destroyed and recreated? I don't see any

solo.getCurrentActivity().onPause()   

or

solo.getCurrentActivity().onDestroy()

methods that I can use? Is this not what Robotium is designed to do?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
MayoMan
  • 4,757
  • 10
  • 53
  • 85

2 Answers2

2

Use a test case base class that has access to Instumentation and then use Instrumentation#callActivityOnPause() and Instrumentation#callActivityOnDestroy().

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
1

This will destroy your activity and create a new one:

    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            activity.recreate();
        }
    });
    setActivity(null);
    activity = getActivity();

This only works on API 11 and up due to the call to "activity.recreate()". If you don't care about saving / restoring instance state in this test, you could call "activity.finish()" instead, which is available on all versions.

ZoFreX
  • 8,812
  • 5
  • 31
  • 51