我有以下活动:
package codeguru.startactivityforresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ChildActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child);
this.resultButton = (Button) this.findViewById(R.id.result_button);
this.resultButton.setOnClickListener(onResult);
}
private View.OnClickListener onResult = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent result = new Intent();
result.putExtra(ChildActivity.this.getString(R.string.result), ChildActivity.this.getResources().getInteger(R.integer.result));
ChildActivity.this.setResult(RESULT_OK, result);
ChildActivity.this.finish();
}
};
private Button resultButton = null;
}
以及以下 JUnit 测试:
package codeguru.startactivityforresult;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import junit.framework.Assert;
public class ChildActivityTest extends ActivityInstrumentationTestCase2<ChildActivity> {
public ChildActivityTest() {
super(ChildActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
this.setActivityInitialTouchMode(false);
this.activity = this.getActivity();
this.resultButton = (Button) this.activity.findViewById(R.id.result_button);
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
@UiThreadTest
public void testResultButtonOnClick() {
Assert.assertTrue(this.resultButton.performClick());
Assert.fail("How do I check the returned result?");
}
private Activity activity;
private Button resultButton;
}
如何确保单击按钮设置正确的结果(通过调用setResult()
)将返回到任何以 开始此活动的活动startActivityForResult()
?