thhVictor
thhVictor

Reputation: 338

Unable to compare int variable in Zxing qr scanner onActivityResult

I am actually trying to intent my fragment to Zxing qr scanner when the button is clicked.

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "PRODUCT_MODE");// for Qr code, its
                                                            // "QR_CODE_MODE"
                                                            // instead of
                                                            // "PRODUCT_MODE"
            intent.putExtra("SAVE_HISTORY", false);// this stops saving ur
                                                    // barcode in barcode
                                                    // scanner app's history
            startActivityForResult(intent, 0);

The problem i am facing is that in the codes below, i am unable to get my resultCode to compare to RESULT_OK and RESULT_CANCELED. I managed to do this in an activity just fine but when i wanted to implement in my fragment class i am unable to do so.

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("onActivityResult", "Started");
        if (requestCode == 0)
        {
            if (resultCode == RESULT_OK)
            {
                String contents = data.getStringExtra("SCAN_RESULT"); 

                TextView txt_qrCode = (TextView) rootView.findViewById(R.id.txt_helloWorld);
//              txt_qrCode.setText(contents);
            }
            else if (resultCode == RESULT_CANCELED)
            {
                // Handle cancel
            }
        }
    }

Upvotes: 2

Views: 357

Answers (1)

thhVictor
thhVictor

Reputation: 338

I found the answer to this question.

int android.app.Activity.RESULT_CANCELED = 0 [0x0]

Because the RESULT_OK and RESULT_CANCELED is actually relying on activity and in my fragment class there is no activity. I simply put

if (resultCode == getActivity().RESULT_OK)
.
.

instead of

if (resultCode == RESULT_OK)
.
.

This would allow me to get the parent activity that inflates the fragment.

Upvotes: 1

Related Questions