onActivityResult() 没有在片段中调用

onActivityResult() is not calling in fragment

我要分段扫描二维码

但是 onActivityResult 没有调用。

Fragment.java

@Override
    public View onCreateView(LayoutInflater inflater ,ViewGroup container ,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate( R.layout.fragment_offer ,container ,false );

        scanOffer = view.findViewById( R.id.scanOffer );

        scanOffer.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scanBarcode();
            }
        } );

        return view;
    }

public void scanBarcode() {
        /** This method will listen the button clicked passed form the fragment **/
        Intent intent = new Intent(getContext(),CaptureActivity.class);
            intent.setAction("com.google.zxing.client.android.SCAN");
            intent.putExtra("SAVE_HISTORY", false);
            startActivityForResult(intent, 0);
    }


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK) {
                uniqueCode = data.getStringExtra("SCAN_RESULT");
                Log.d(TAG, "contents: " + uniqueCode);
                Toast.makeText( getContext() ,uniqueCode ,Toast.LENGTH_SHORT ).show();
//                callAddStoreContestParticipantService();
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.d(TAG, "RESULT_CANCELED");
            }
        }
    }

请帮帮我。

onActivityResult() 没有调用

CaptureActivity.class扫描后打开Qr onActivityResult()未调用

你必须打电话给 getActivity().startActivityForResult(intent, 0); 在你的片段中 在你的 activity 你在 onActivityResultMethod() 你必须打电话给 yourfragmnt.onActivityResult()

尝试使用以下代码进行条形码扫描,并在 activity 上重写父 activity

结果
    private static final int BARCODE_REQUEST = 312;
private void startBarcode() {
    //IntentIntegrator.forFragment(getActivity().initiateScan()); // `this` is the current Fragment

    IntentIntegrator integrator = new IntentIntegrator(getActivity()) {
        @Override
        protected void startActivityForResult(Intent intent, int code) {
          Fragment.this.startActivityForResult(intent, BARCODE_REQUEST); // REQUEST_CODE override
        }
    };
    //IntentIntegrator integrator = new IntentIntegrator(getActivity());
    //IntentIntegrator.forSupportFragment(this);
    integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
    integrator.setPrompt("Scan a barcode");
    integrator.setCameraId(0);  // Use a specific camera of the device
    integrator.setBeepEnabled(true);
    integrator.setBarcodeImageEnabled(true);
    integrator.setOrientationLocked(false);
    integrator.setTimeout(15000);
    integrator.initiateScan();
}

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {

        case BARCODE_REQUEST:
            IntentResult Result = IntentIntegrator.parseActivityResult(IntentIntegrator.REQUEST_CODE, resultCode, data);
            if (Result != null) {
                if (Result.getContents() == null) {
                    Timber.i("cancelled scan");
                    showSnackbar("cancelled scan", true);

                } else {
                    Timber.i("Scanned");
                    showSnackbar("Code scan successfully", false);


                    try {


        long id = Long.parseLong(Result.getContents());
//                            getFood(id);

                            searchBarcode(Result.getContents());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


//                    searchBarcode(Result.getContents());
                        //getFood(Long.valueOf(mItem.get(position - 1).getID()));
                    }
                } else {
                    showSnackbar("Barcode not scanned", true);
                    Timber.i("Barcode Result is NULL");
                    super.onActivityResult(requestCode, resultCode, data);
                }
                break;
        }

    }

你可以参考这个例子:Barcode Scanner