Q:

Android Test Framework performClick() not starting Activity

I have an ActivityInstrumentationTestCase2 with a test executing Button.click(). The Button should start an other Activity to do some work.
I think Button.performClick() is performed correctly, but the test is finishing before the other Activity is executed.
@UiThreadTest
public void test() {

    Intent i = new Intent(this.myActivity, MyActivity.class);
    myActivity.startActivity(i);    

    Button button = (Button) myActivity.findViewById(R.id.button);
    button.performClick();

}

I tried the following which worked but i think this is rather a work-around than a good solution.
public void test() {

    Intent i = new Intent(this.myActivity, MyActivity.class);
    myActivity.startActivity(i);    

    Button button = (Button) myActivity.findViewById(R.id.button);
    button.performClick();

    try {
    Thread.sleep(50000);
    } catch (InterruptedException e) {
        Log.e("MyTest", e.getMessage());
    }

}

Isn´t there a better way?

A:

This is my final solution:
public void test() {

    Instrumentation instrumentation = getInstrumentation();

    // Prepare a monitor for your activity
    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(MyActivity.class.getName(), null, false);

    // Start your activity manually
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(instrumentation.getTargetContext(), MyActivity.class.getName());
    instrumentation.startActivitySync(intent);

    Activity myActivity = getInstrumentation().waitForMonitor(monitor);

    Button upSend = (Button) myActivity.findViewById(R.id.button);
    upSend.performClick();

    Log.d("MyTest", "button clicked");

    //wait for SecondActivity to start (called by MyActivity)
    monitor = instrumentation.addMonitor(SecondActivity.class.getName(), null, false);
    Activity secondActivity = getInstrumentation().waitForMonitor(monitor);

    int count = 0;

    //wait until SecondActivity is finishing
    while(!secondActivity.isFinishing()) {

        Log.d("MyTest", "waiting - " + ++count);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

            Log.e("MyTest",e.getMessage());
        }
    }

}

Thx again to Erik; now the second activity gets started by button and test is waiting for it to finish.

