Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setResult does not work when BACK button pressed

Tags:

android

I am trying to setResult after the BACK button was pressed. I call in onDestroy

Intent data = new Intent(); setResult(RESULT_OK, data)  

But when it comes to

onActivityResult(int requestCode, int resultCode, Intent data)  

the resultCode is 0 (RESULT_CANCELED) and data is 'null'.

So, how can I pass result from activity terminated by BACK button?

like image 872
alex2k8 Avatar asked Apr 20 '10 22:04

alex2k8


1 Answers

You need to overide the onBackPressed() method and set the result before the call to superclass, i.e

@Override public void onBackPressed() {     Bundle bundle = new Bundle();     bundle.putString(FIELD_A, mA.getText().toString());          Intent mIntent = new Intent();     mIntent.putExtras(bundle);     setResult(RESULT_OK, mIntent);     super.onBackPressed(); } 
like image 170
n224576 Avatar answered Sep 18 '22 10:09

n224576