-1

I have an app for qrcode scanner. What happens with my code in fragment is, it just opens the camera and scan and did not toast any results.. I don't see any problem with my code because when I transferred it to an activity, it is working well.

It looks like my onActivityResult is not called in Fragments.

Tried checking this post but it didn't help..

// ...........

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id){
        case R.id.btn_chargeitem:
            Intent intent = new Intent(getActivity(), AllPurchaseActivity.class);
            startActivity(intent);
            break;
        case R.id.scanqrcode_layout:
            scanQrCode();
            break;
    }
}

public void scanQrCode(){
    IntentIntegrator integrator = new IntentIntegrator(getActivity());
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
    integrator.setPrompt("Scan");
    integrator.setCameraId(0);
    integrator.setBeepEnabled(true);
    integrator.setBarcodeImageEnabled(false);
    integrator.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(getActivity(), "Scanning was cancelled", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(getActivity(), result.getContents()+"are the contents of the qrcode", Toast.LENGTH_SHORT).show();
            }
        }
        else {
            super.onActivityResult(requestCode, resultCode, data);
        }
}

// ............
Natig Babayev
  • 3,128
  • 15
  • 23
no_profile
  • 387
  • 5
  • 15
  • 2
    Have you overridden `onActivityResult()` in the `Activity`? If so, did you make sure to call `super.onActivityResult()` there? – Mike M. Sep 12 '19 at 08:27
  • Have you override the `onActivityResult` in the Activity and see if it is coming there? – Rahul Khurana Sep 12 '19 at 08:28
  • @MikeM. thanks for your reply. The problem that I have is in my Fragment, not in my Activity – no_profile Sep 12 '19 at 08:29
  • 1
    Yes, I realize that. But the `Fragment`'s `onActivityResult()` will not be called if you've overridden that method in the `Activity` without calling the `super` method. – Mike M. Sep 12 '19 at 08:30
  • @MikeM. I have already overidden the onActivityResult().. – no_profile Sep 12 '19 at 08:32
  • Also, it looks like you need to use `IntentIntegrator.for[Support]Fragment(this)` instead of `new IntentIntegrator(getActivity())`. – Mike M. Sep 12 '19 at 08:34

2 Answers2

1

In Activity (the one that hosts the fragment): override the onActivityResult() method and call super.onActivityResult. Calling super is mandatory! It won’t work otherwise. So this is the magic solution, to override and call super for onActivityResult method inside the hosting activity of your fragment.

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Override this method in the activity that hosts the Fragment and call super
        // in order to receive the result inside onActivityResult from the fragment.
        super.onActivityResult(requestCode, resultCode, data);
    }

In Fragment

`override onActivityResult()` and let your logic here (if this is what you wanted)
when starting the activity use just startActivityForResult() and not getActivity.startActivityForResult()
Emad Seliem
  • 608
  • 1
  • 4
  • 5
0

If you have overridden onActivityResult() in your Activity, then you have to add supper.onActivityResult(), Like following.

@Override
protected void onActivityResult(int requestCode, int resultCode,
  Intent intent) {
     // add this line 
     super.onActivityResult(requestCode, resultCode, intent);
     // your other code here
}
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29