Dan Jackson
Dan Jackson

Reputation: 13

Using ZXing in a fragment

I have searched all over the place and I can't find a working solution on how to implement ZXing in a Fragment. Multiple sources have told me that this is right, but onActivityResult never gets called.

The button click triggers the scanner to open

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    view = inflater.inflate(R.layout.fragment_barcode,container,false);
    final Button openBC = (Button)view.findViewById(R.id.btnOpen);
    openBC.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            //This opens up the Barcode Scanner
            IntentIntegrator integrator = new IntentIntegrator(getActivity());
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scan A barcode or QR Code");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });


    return view;
}

This is where the result of the scan should be processed, but it never gets here

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Log.d("MainActivity", "Cancelled scan");
            Toast.makeText(getActivity(), "canceled",Toast.LENGTH_LONG).show();
        } else {
            Log.d("MainActivity", "Scanned");
            Toast.makeText(getActivity(),"Scanned: " + result.getContents(),Toast.LENGTH_LONG).show();
        }
    }else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Upvotes: 1

Views: 5787

Answers (3)

KevinZR
KevinZR

Reputation: 11

My solution for this problem is this:

 IntentIntegrator  i =IntentIntegrator.forSupportFragment(SlideshowFragment.this);
            i.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            i.setPrompt("ESCANEAR CODIGO");
            i.setCameraId(0);
            i.setOrientationLocked(false);
            i.getMoreExtras();
            i.setBeepEnabled(true);
            i.setBarcodeImageEnabled(false);
            i.initiateScan();

Upvotes: 0

Navneet
Navneet

Reputation: 81

If the fragment is imported from import android.support.v4.app.Fragment then you should use IntentIntegrator.forSupportFragment(this).initiateScan();

If the framgment is imported from import android.app.Fragment then IntentIntegrator.forFragment(MyFragment.this).initiateScan(); can be used..

Upvotes: 2

an_droid_dev
an_droid_dev

Reputation: 1136

This happen because you override onActivityResult from Fragment but initialize intentIntegrator IntentIntegrator integrator = new IntentIntegrator(getActivity()); with activity.

You are two options: 1) Override onActivityResult inside Activity 2) Change initialization passing fragment inside activity instance.

This should be help you:

Assuming that fragment is called MyFragment:

IntentIntegrator.forFragment(MyFragment.this).initiateScan(); 

ZXing Minimal

Upvotes: 2

Related Questions