
上QQ阅读APP看书,第一时间看更新
How to do it...
There are only a few changes needed to get the results:
- First of all, open MainActivity.java and add the following constant to the class:
public static final String REQUEST_RESULT="REQUEST_RESULT";
- Next, change the way the intent is called by modifying the onClickSwitchActivity() method to expect a result:
public void onClickSwitchActivity(View view) {
EditText editText = (EditText)findViewById(R.id.editTextData);
String text = editText.getText().toString();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT,text);
startActivityForResult(intent,1);
}
- Then, add this new method to receive the result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK) {
Toast.makeText(this, Integer.toString(data.getIntExtra(REQUEST_RESULT,
0)), Toast.LENGTH_LONG).show();
}
}
- Finally, modify onClickClose in SecondActivity.java to set the return value as follows:
public void onClickClose(View view) { Intent returnIntent = new Intent(); returnIntent.putExtra(MainActivity.REQUEST_RESULT,42); setResult(RESULT_OK, returnIntent); finish(); }