0

I'am trying to read the barcode via Zxing in fragment

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

The issue is it doesn't access the onActivityResult

Yagami Light
  • 1,756
  • 4
  • 19
  • 39

2 Answers2

1

The hosting activity overrides onActivityResult(), but it did not make a call to super.onActivityResult() for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult() call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult() for all unhandled results, the fragment got a shot at handling the result.

Check this out:

onActivityResult is not being called in Fragment

I hope to be useful for u :)

hamid keyhani
  • 451
  • 3
  • 12
1

The solution was quiet simple the onActivityResult that i implemented was Overrided from the parent activity

The solution is to call the fragment onActivityResult from the parent activity

private static final int BARECODE_REQUEST = 114910;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     

    if (requestCode == BARECODE_REQUEST) {
        super.onActivityResult(requestCode,resultCode,data);
    }
}
Yagami Light
  • 1,756
  • 4
  • 19
  • 39