Search code examples
androidqr-codezxing

ZXing not calling onActivityResult, When scanning QR Code


I'm using the ZXing Android Embedded library to read a QRCode and the onActivityResult is not called after the QRCode is successfully scanned, please help.

public class ScanActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_money);

        Button scanButton = (Button) findViewById(R.id.scan_button);
        scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator integrator = new IntentIntegrator(ScanActivity.this);
                integrator.initiateScan();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("*************** Why is this method not called?");
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Result is: ", Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

Solution

  • The problem with this code is that I had used android:noHistory="true" on ScanActivity in the manifest file

    <activity
        android:name=".ScanActivity" android:noHistory="true" />
    

    Removing android:noHistory="true" solved the problem

    <activity
        android:name=".ScanActivity" />