1

I have been looking for an answer all over the internet.

The thing is, i found many ways to implement a QR code scanner in my app, in an activity.

This is one of the ways:

        scan_btn = (Button) view.findViewById(R.id.scan_btn);
    scan_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            IntentIntegrator integrator = new IntentIntegrator(getActivity());
            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            integrator.setPrompt("Scan!!");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });    

Now i want to get it to work in a Fragment. The problem is, it starts a new activity (the QR code reader) Scans the QR code But i dont get a response in my onActivityResult:

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

    if (result != null) {
        if (result.getContents() == null) {
            System.out.println("Cancelled");
            Toast.makeText(getActivity(), "You cancelled the scanning!", Toast.LENGTH_LONG).show();
        } else {
            System.out.println("Worked: " + result.getContents());
            Toast.makeText(getActivity(), "scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

But what is going wrong?

I guess it has to do with this part:

  IntentIntegrator integrator = new IntentIntegrator(getActivity());       

It gets the activity, but it is a fragment, instead of an activity. How can i solve this problem?

Communicating first to my Activity which holds the fragment and then get the result ? Please help, Thanks :)

Alex Hakman
  • 62
  • 1
  • 8
  • Just pass fragment instead of getActivity in constructor – Nayan Srivastava Nov 21 '16 at 17:05
  • Maybe you will find the answer in this post. https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment Otherwise you have to share a bit more information about your code. Is the button ant the attached to the fragment or the activity itself? Is the shown onActivityResult method implemented in your fragment class? – Xerox23 Nov 21 '16 at 16:56

2 Answers2

2

I will presume that the implementation of the onActivityResult is on your Fragment, right?

The IntentIntegrator implementation on your Fragment is right. So, just remove your onActivityResult code from the Fragment and put it on the Activity.

I had a similar problem and this was my solution.

Buddy
  • 10,874
  • 5
  • 41
  • 58
0
IntentIntegrator intentIntegrator=
IntentIntegrator.forSupportFragment(FragmentNme.this);

It worked for me to solve my problem...rest all code is same.

Jaimil Patel
  • 1,301
  • 6
  • 13