To capture actions performed on one Activity within another requires three steps.
Launch the secondary Activity (your 'Edit Text' Activity) as a subactivity by using startActivityForResult
from your main Activity.
Intent i = new Intent(this,TextEntryActivity.class);
startActivityForResult(i, STATIC_INTEGER_VALUE);
Within the subactivity, rather than just closing the Activity when a user clicks the button, you need to create a new Intent and include the entered text value in its extras bundle. To pass it back to the parent call setResult
before calling finish
to close the secondary Activity.
Intent resultIntent = new Intent();
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, enteredTextValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();
The final step is in the calling Activity: Override onActivityResult
to listen for callbacks from the text entry Activity. Get the extra from the returned Intent to get the text value you should be displaying.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (STATIC_INTEGER_VALUE) : {
if (resultCode == Activity.RESULT_OK) {
String newText = data.getStringExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
// TODO Update your TextView.
}
break;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…