I have one activity with that launches another activity that has a EditText-view. When the user clicks the “Back” button I want the string in this edit text view to be returned but I can’t get it to work. I tried to implement a button (test purpose) and running my code and then it works.
In the activity that launches the other one:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (REQUEST_CODE_COMMENT == requestCode) {
if (RESULT_OK == resultCode) {
Toast.makeText(this, data.getDataString(), Toast.LENGTH_LONG).show();
}
}
}
The activity with the edit text view (I want this to work):
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.setData(Uri.parse("hfldskajfkj" + commentEditText.getText().toString()));
setResult(RESULT_OK, intent);
}
But the resultCode is 0 (RESULT_CANCELLED) when i log it.
Thats the default implementation of
onBackPressed()(also mentioned in the documentation). This means your activity gets finished (with the default codeRESULT_CANCELLED) before your other code gets invoked. Removingsuper.onBackPressed()and addingfinish()on the bottom should fix this.