为什么在活动开始之前就触发了onActivityResult? [英] Why is onActivityResult triggered before activity even starts?

查看:116
本文介绍了为什么在活动开始之前就触发了onActivityResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要扫描条形码以获取代码才能继续的应用程序.我使用以下代码开始扫描活动:

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 */
            }
        });

上面的代码在标有finished的按钮上设置了一个侦听器.单击该按钮时,它会显示提示,要求用户扫描条形码或取消条形码.

The above code 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 activity result in a toast message.

我遇到的问题是,一旦按下扫描条形码按钮,就会触发onActivityResult方法.

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

我可以在logcat中看到扫描结果,因此扫描过程可以正常工作,但是由于onActivityResult方法触发得太早,所以它从未获得扫描结果,并且结果代码始终为-1

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

我在这里错过了一步吗?是否有办法进入onActivityResult等待活动实际完成?

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

推荐答案

似乎我正在使用的条形码扫描器在清单中具有单个实例的选项卡界面中存在一个特定的错误,这就是我的处理方式.

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.

这篇关于为什么在活动开始之前就触发了onActivityResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆