未调用OnActivityResult侦听器

时间:2015-01-07 17:54:31

标签: android zxing

有人可以帮助我理解并解决为什么在下面的代码中没有调用OnActivityResultListener,我已经检查了错误日志,我无法找到解决方案

MainActivity

public void onClick(View v) {
// TODO Auto-generated method stub
//Handling multiple scan modes based on selection
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
if (type == "QR_CODE_MODE"){
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
}else if(type == "PRODUCT_MODE"){
    intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
}else {
    intent.putExtra("SCAN_MODE", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,PDF417,RSS_EXPANDED");
}
startActivityForResult(intent, 0);

}


public void OnActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Toast.makeText(getBaseContext(), requestCode, Toast.LENGTH_LONG).show();
 if (requestCode == 0) {
        if (resultCode == RESULT_OK){
        //we have a result
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            formatTxt.setText("FORMAT: " + contents);
            contentTxt.setText("CONTENT: " + format);
        }
     else if (resultCode == RESULT_CANCELED) {
         // Handle cancel
         formatTxt.setText("FORMAT: No results found");
         contentTxt.setText("CONTENT: No results found");
         Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
         toast.setGravity(Gravity.TOP, 25, 400);
         toast.show();
    }
   }
}

2 个答案:

答案 0 :(得分:2)

更改

public void OnActivityResultListener(int requestCode, int resultCode, Intent intent){

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent){

onActivityResult()是您可以在Activity中覆盖的方法,@Override可以自动检查该方法是否覆盖了超类型中的方法。

答案 1 :(得分:2)

  

为什么不调用OnActivityResultListener

因为OnActivityResultListener(是PreferenceManager中的接口)不是在子活动完成时调用父活动的方法。

您可以通过在父活动中实施OnActivityResultListener并将onActivityResult覆盖为:

来获取父活动的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    ...do your work here
}