onActivityResult()未在片段中调用

时间:2019-06-14 10:15:34

标签: android android-fragments fragment android-camera onactivityresult

我想分段扫描QR码。

但是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 在扫描onActivityResult()未调用后打开Qr

2 个答案:

答案 0 :(得分:0)

尝试下面的代码进行条形码扫描,并在父活动中覆盖活动结果

    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

答案 1 :(得分:-1)

您必须致电getActivity().startActivityForResult(intent, 0); 在你的片段 并且在您的活动中,您在onActivityResultMethod()中必须致电yourfragmnt.onActivityResult()