Q:

back to previous activity with intent error

I have two activity, on second activity, when  I press enter it will get some value and bring back to first activity.
This is my code:
second activity
//event on "ENTER" button
Intent i = new Intent();
i.putExtra("namaDokter", "kosong");
i.putExtra("idDokter", "kosong");
setResult(RESULT_OK, i);
finish();

get its value on first activity
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
    namaDokter=data.getStringExtra("namaDokter");
    idDokter=data.getStringExtra("idDokter");
}

but if I don't press "ENTER" button and only press back button from hardware(or Esc key on my keyboard when I'm using emulator), my app crash, I think it's because I don't get any value from data.getStringExtra("namaDokter");
what should I do?

A:

you need to check if you got RESULT_OK or not
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
  if(ResultCode == RESULT_OK) {

    namaDokter=data.getStringExtra("namaDokter");
    idDokter=data.getStringExtra("idDokter");
  }else
    //You pressed back
}

