OnActivityResult 监听器未被调用

OnActivityResult Listener is not called

有人可以帮助我理解和解决为什么在下面的代码中没有调用 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();
    }
   }
}

改变

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

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

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

why the OnActivityResultListener is not called

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

您可以通过在父级 Activity 中实现 OnActivityResultListener 并将 onActivityResult 重写为:

来在父级 Activity 中获得结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    ...do your work here
}