无法访问片段的onActivityResult zxing

问题描述 投票:0回答:1

我正在尝试通过片段中的Zxing读取条形码

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_caddie, container, false);
    etCodigo = v.findViewById(R.id.etCodigo);
    btnLeerCodigo = v.findViewById(R.id.btnLeerCodigo);

    btnLeerCodigo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            escanear();
        }
    });
    text = "";

    return v;
}


public void escanear() {

    IntentIntegrator intent = IntentIntegrator.forSupportFragment(FragmentCaddie.this);
    //IntentIntegrator intent = new IntentIntegrator(getActivity());
    intent.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
    intent.setPrompt("ESCANEAR CODIGO");
    intent.setCameraId(0);
    intent.setBeepEnabled(false);
    intent.setBarcodeImageEnabled(false);
    intent.initiateScan();

}


 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(getContext(), "Cancelaste el escaneo", Toast.LENGTH_SHORT).show();
        } else {
            text = text + " + " +  result.getContents().toString() ;
            etCodigo.setText(text);
            escanear();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

问题是它无法访问onActivityResult

android android-fragments barcode zxing android-fragmentactivity
1个回答
0
投票

托管活动会覆盖onActivityResult(),但未对未处理的结果代码调用super.onActivityResult()。显然,即使片段是进行startActivityForResult()调用的片段,该活动也会在处理结果时获得第一枪。当您考虑片段的模块化时,这很有意义。一旦为所有未处理的结果实现了super.onActivityResult(),该片段就可以处理结果了。

查看此:

onActivityResult is not being called in Fragment

我希望对您有用:)

© www.soinside.com 2019 - 2024. All rights reserved.