Reputation: 156
In my main activity I call the second activity via button click in my toolbar like this
if(id == R.id.addNav) {
startActivityForResult(new Intent(this, TextFormActivity.class), 0);
return true;
}
Then in my second activity first I have constant for the tag
public static final String EXTRA_TEXT_ADD =
"text_add";
Then when the add button was clicked I'm setting up the result
mButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Text text = new Text();
text.setTitle(mEditTextTitle.getText().toString());
text.setRecipient(mEditTextRecipient.getText().toString());
text.setSent(false);
text.setMessage(mEditTextMessage.getText().toString());
mTextList.add(text);
Intent i = new Intent();
i.putExtra(TextFormActivity.EXTRA_TEXT_ADD,true);
setResult(RESULT_OK, i);
}
});
This is how I handle things in onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("TextListActivity", "Result Entered");
if(data == null){
return;
}
boolean result = data.getBooleanExtra(TextFormActivity.EXTRA_TEXT_ADD,false);
Toast.makeText(this,"Result Code: "+ result,Toast.LENGTH_SHORT).show();
}
I still don't know why my toast is not showing up when I'm returning to the main activity
EDIT Well I already found the problem and that is I'm using NavUtils.navigateUpFromSameTask(TextFormActivity.this); to go back to the main activity and it does not call the method onActivityResult.
Upvotes: 0
Views: 1148
Reputation: 16317
You need to finish the activity you started for result.
So in you button click listener after setting the result , just finish the activity.
mButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
.....
........
Intent i = new Intent();
i.putExtra(TextFormActivity.EXTRA_TEXT_ADD,true);
setResult(RESULT_OK, i);
finish();// finish the activity
}
});
Try this, should work.
Upvotes: 1