views:

258

answers:

3

Hi all,

I have an app that needs to scan a barcode to get a code before it can continue.

I use this code to start the scanning activity:

finish = (Button) findViewById(R.id.finishButton);
        finish.setOnClickListener(new OnClickListener() {
            public void onClick(View viewParam) {

                /*Prompt the user to scan the barcode */
                new AlertDialog.Builder(Visit.this)
                .setMessage("Please Scan the clients barcode to complete the visit")
                .setPositiveButton("Scan Barcode", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Start the scan application
                        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                        startActivityForResult(intent, 0);
                    }
                })

                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Execute some method call
                        Toast.makeText(Visit.this, "Scan declined...", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
                /* End of Scan prompt */
            }
        });

What the above code does is sets a listener on a button labeled "finished" When the button is clicked, it displays a prompt asking the user to scan a barcode or cancel.

Clicking on the 'Scan Barcode' button starts a new activity which starts the scan.

I have the following code set up to read the result of the scan on the return from the scan:

/* Return from scanning barcode */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
;
      if (resultCode == Activity.RESULT_OK && requestCode == 0) {
        Bundle extras = data.getExtras();
        String result = extras.getString("SCAN_RESULT");
      }
      Toast.makeText(Visit.this, "request code: "+requestCode+" result code = "+resultCode+ "\nRESULT_OK: "+Activity.RESULT_OK, Toast.LENGTH_SHORT).show();
    }

Which very simply (for now) outputs the avtivity result in a toast message.

The problem Im having is that the onActivityResult method is being triggered as soon as I press the scan barcode button.

The scanning process works fine as I can see the results of the scan in the logcat., however because it is triggered too soon the onActivityResult method never gets the scan result and the result code is always -1

Am I missing a step here? is there someway of getting to onActivityResult to wait until the activity actual finishes?

Thanks

Kevin

A: 

You can't use startActivityForResult() whithought using an explicit intent. That is, you can't use it for running Activities that AREN'T in your own activity. If you're not passing in this to the Intent, you probably can't use startActivityForResult

Edit after misunderstanding:

I think it's because you're calling startActivityForResult from a Dialog. The Dialog is essentially an activity, so THAT DIALOG is the one that will be receiving the callback from onActivityResult (I think)

Falmarri
Not sure I follow, I am passing an intent: Intent intent = new Intent("com.google.zxing.client.android.SCAN"); startActivityForResult(intent, 0);This is code I found from another question on here. http://stackoverflow.com/questions/2050263/using-zxing-to-create-an-android-barcode-scanning-app
Kevin Bradshaw
Sorry I misunderstood. I'm not familiar with this particular barcode scanner. Can you post your full code?
Falmarri
A: 

Never mind, it seems like the barcode scanner I am using has a particular bug in it for tab interfaces with single instance in the manifest which is how I had it.

Removing the single instance stipulation allowed me to do a hack on it thats working perfectly.

Oh, well you live and learn.

Thanks

Kevin

Kevin Bradshaw
A: 

I have the same issue, how do we make this 'hack' in the project?

Alex