Android的onActivityResult触发活动,甚至开始前! [英] Android onActivityResult triggered before activity even starts!

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

问题描述

我有一个应用程序需要扫描棒code得到code才能继续。

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

我用这个code键启动扫描活动:

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

在什么上面code的作用是设置上标有一个按钮,一个监听器成品 当单击该按钮,它会显示一个提示,询问用户扫描吧code或取消。

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.

点击扫描吧code按钮启动该启动扫描新的活动。

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

我有以下的code设念从扫描返回的扫描结果:

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();
    }

这非常简单(现在)输出avtivity结果举杯消息。

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

林遇到的问题是,onActivityResult方法被立即触发我preSS扫描吧code按钮。

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

扫描过程工作正常,因为我可以看到在logcat中扫描的结果,而是因为它是触发太快了onActivityResult方法永远不会扫描结果,结果code是始终是-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?

感谢

凯文

推荐答案

没关系,好​​像我现在用的是吧code扫描仪中有与单实例选项卡接口,一个特定的错误在清单中是怎么我了吧。

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.

感谢

凯文

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

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