Search code examples
androidfragmentzxing

Zxing, call in Fragment and get Result


I have the following problem: I´m trying to launch my scanner from an fragment. But actually it´s not possible to catch the result via onActivityResult in this fragment.

    @Override
public void onClick(View v){


    IntentIntegrator integrator = new IntentIntegrator(getActivity());

    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
    integrator.setCaptureActivity(HorizontalActivity.class);
    integrator.setOrientationLocked(false);
    integrator.initiateScan();


}

I also tried the code from the example

IntentIntegrator.forFragment(getActivity())

But my problem is that I am using the .v4.App.FragmentActivity and the Konstruktor is only for App.Fragment.

Maybe you could help me please?


Solution

  • Try this inside your fragment....

    IntentIntegrator.forFragment(this).initiateScan();
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if(result != null) {
                if(result.getContents() == null) {
                   //Cancelled
                } else {
                   //"Scanned Result=" + result.getContents()
                }
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
    
        }