0

I have an activity, called ScanRouter, that calls ZBar Scanner via startActivityForResult. Once I scan a code, onActivityResult in ScanRouter processes the result, passes the result to another activity via intents and ScanRouter is "finish()".

However, after the result is passed to the new activity, if the back button is pressed, ScanRouter reopens ZBar. If I set ScanRouter to noHistory=true, then onActivityResult doesn't work.

How can I prevent ScanRouter from firing again if the back button is pressed? I thought finish() would remove it from the stack but that is apparently not true. I would like to go back to the activity that originally called ScanRouter rather than ScanRouter itself.

ScanRouter: Portion of the code that matters

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case ZBAR_SCANNER_REQUEST:
        case ZBAR_QR_SCANNER_REQUEST:
        if (resultCode == RESULT_OK) {
            // TODO go back to the caller and give them the result
            String value = data.getStringExtra(ZBarConstants.SCAN_RESULT);
            **returnIntent(value);**
        } else if(resultCode == RESULT_CANCELED && data != null) {
            // TODO go back to the caller and give them the error
            String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
            if(!TextUtils.isEmpty(error)) {
                Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
            }
        }
        break;
    }
}

protected void returnIntent(String value) {
    Intent intent = new Intent(this, Manager.class);
    intent.putExtra("operation", caller);
    intent.putExtra("code", value);
    startActivity(intent);
    **this.finish();**
}
jroot.zen
  • 231
  • 1
  • 3
  • 9
  • Could you post the code where you pass the result to a new activity and call finish() on ScanRouter please? – Cata Jun 25 '13 at 19:16
  • @Cata I added the relevant code... Thanks for reviewing this. – jroot.zen Jun 26 '13 at 11:52
  • The code seems fine, are you sure that you're not opening the ScanRouter twice? – Cata Jun 26 '13 at 11:55
  • @Cata Yeah... I've checked it several times now. The flow is that another activity presents a fragment. User action causes ScanRouter to fire which opens ZBar. Result is returned and sent back to the fragment that needs it. If you hit back at this point, you'll refire ScanRouter which reopens ZBar. If you continue forward through the operations, it seems to get removed from the stack. But it doesn't seem to shut down ScanRouter when I want it to... – jroot.zen Jun 26 '13 at 12:22

1 Answers1

1

Try put finish() at the end of returnIntent instead this.finish();

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
jekeyeke
  • 301
  • 1
  • 3
  • 11